编译错误:将值从工作表2传输到工作表1时,引用无效或不合格

时间:2018-09-25 17:04:40

标签: excel vba

我的数据中的范围应该正确,因为它们适用于复制,但是我只是想获取sheets(2)sheets(1)的指定范围内的值

Private Sub insertData(RowStartData1 As Long, RowNum1 As Long)
' RowStartData1 is where the row the data starts on.
' RowNum1 is the row num - 1 where data should be inserted

Dim ColNum As Integer

RowNum1 = RowNum1 + 1
' adds one to row num so that insertion happens at right place
For ColNum = 2 To 9
        Sheets(1).Range(.Cells(RowNum1, ColNum), .Cells(RowNum1 + 5, ColNum)).Value = 
Sheets(2).Range(.Cells(RowStartData1, 4), .Cells(RowStartData1 + 5, 4)).Value

    RowStartData1 = RowStartData1 + 6

Next ColNum

End Sub

1 个答案:

答案 0 :(得分:4)

enter image description here

您遇到编译错误。您需要显式或在With块中限定With ActiveSheet ' ## Modify as needed ' adds one to row num so that insertion happens at right place For ColNum = 2 To 9 Sheets(1).Range(.Cells(RowNum1, ColNum), .Cells(RowNum1 + 5, ColNum)).Value = _ Sheets(2).Range(.Cells(RowStartData1, 4), .Cells(RowStartData1 + 5, 4)).Value RowStartData1 = RowStartData1 + 6 Next ColNum End With 的资格,例如:

Action