2016-10-02 27 views
0

我有一张表,我们称之为sheetA。我在该工作表中有一系列字段(rangeA),其中的公式可以确定同一工作表中的两个范围。我们称它们为rangeBrangeC。一旦确定这些,我想分别将rangeBrangeC分别复制到sheetBsheetC。一旦完成,我想删除rangeA。重新排序,以便我可以手动输入该范围内的新值并重复该过程。在VBA中复制,粘贴和删除相关范围到不同的表单

我想有一个功能/按钮,可以实现这一点。我曾尝试以下:

Private Sub TransferPuzzleButton1_Click() 

    FirstOperation 
    GetFirstEmptyCell1 "sht As Worksheet", "row As Long" 
    SecondOperation 
    GetFirstEmptyCell1 "sht As Worksheet", "row As Long" 
    ClearCell 

End Sub 

Sub FirstOperation() 
    Dim sourceSht As Worksheet: Set sourceSht = ThisWorkbook.Worksheets(1) 
    Dim destSht As Worksheet: Set destSht = ThisWorkbook.Worksheets(2) 

    GetFirstEmptyCell(destSht, 1).Resize(25).Value = sourceSht.Range("A1:A27").Value 
End Sub 

Function GetFirstEmptyCell1(sht As Worksheet, row As Long) As Range 
    Set GetFirstEmptyCell = sht.Cells(1, sht.Columns.Count).End(xlToLeft) 
    If Not IsEmpty(GetFirstEmptyCell) Then Set GetFirstEmptyCell = GetFirstEmptyCell.Offset(, 1) 

End Function 
Sub SecondOperation() 
    Dim sourceSht As Worksheet: Set sourceSht = ThisWorkbook.Worksheets(1) 
    Dim destSht As Worksheet: Set destSht = ThisWorkbook.Worksheets(3) 
    GetFirstEmptyCell(destSht, 1).Resize(2).Value = sourceSht.Range("C1:C2").Value 

End Sub 
Function GetFirstEmptyCell2(sht As Worksheet, row As Long) As Range 
    Set GetFirstEmptyCell = sht.Cells(1, 2).End(xlToLeft) ' 
    If Not IsEmpty(GetFirstEmptyCell) Then Set GetFirstEmptyCell = GetFirstEmptyCell.Offset(, 1) 
End Function 
Sub ClearCell() 
    Dim sourceSht As Worksheet: Set sourceSht = ThisWorkbook.Worksheets(1) 

    sourceSht.Range("F7:I10").Clear 
    sourceSht.Range("C1:C2").Clear 
End Sub 

看来我压延Sub调用不知何故

回答

0

随着GetFirstEmptyCell1 "sht As Worksheet", "row As Long"你试图调用子接受参数的开始。参数输入是在sub的定义中完成的,而不是在调用sub的语句中完成的。

当你调用sub时,你需要提供它所期望的参数,在本例中是一个工作表和一个long。

因此,在调用子之前准备好数据。缩写:

Private Sub TransferPuzzleButton1_Click() 

Dim mySheet As Worksheet 
dim myNumber as Long 
Set mySheet = ThisWorkbook.Worksheets("DemoSheet") 
MyNumber = 1000 

' now call the function 

    GetFirstEmptyCell1 mySheet, myNumber 

End Sub 


    Function GetFirstEmptyCell1(sht As Worksheet, row As Long) As Range 
     Set GetFirstEmptyCell = sht.Cells(1, sht.Columns.Count).End(xlToLeft) 
     If Not IsEmpty(GetFirstEmptyCell) Then Set GetFirstEmptyCell = GetFirstEmptyCell.Offset(, 1) 

    End Function 

顺便说一句,您在函数内使用的名称是不一致的。 GetFirstEmptyCell1与GetFirstEmptyCell。

相关问题