我试图编写一个vba代码来检查工作簿(A).worksheet(a).cells(row1,colum1)= workbook(B).worksheet(b).cells(row2,colum2) ,然后做一些复制和进一步检查。 但遇到错误信息"下标超出范围"。 有人可以帮我这个吗?
以下是代码:
Sub check()
Dim row1, row2, row1start, row2start, row1end, row2end
Dim ws1 As Worksheet
ws1 = Excel.Workbooks("A.xlsx").Worksheets("sheet1")
Dim ws2 As Worksheet
ws2 = Excel.Workbooks("B.xlsx").Worksheets("sheet2")
row1start = 2
row1end = 43
row2start = 3
row2end = 163
For row1 = row1start To row1end
For row2 = row2start To row2end
If ws2.Cells(row2, "I") = ws1.Cells(row1, "H") Then
ws1.Cells(row1, "I") = 0 'mark true
ws1.Cells(row1, "M") = ws2.Cells(row2, "K") 'copy
If ws1.Cells(row1, "C") = ws2.Cells(row2, "J") Then
ws1.Cells(row1, "N") = 0 'mark true
Else
ws1.Cells(row1, "N") = ws2.Cells(row2, "J") 'copy
End If
Exit For 'if found, then exit 1st loop
End If
Next row2
If row2 >= row2end Then 'if no match, assign value -1
ws1.Cells(row1, "I") = -1
End If
If row2start < row2end Then 'reduce searching times for next row1
row2start = row2 + 1
End If
Next row1
End Sub
答案 0 :(得分:0)
下标超出范围意味着它无法找到指定的工作簿,工作表或范围。检查,仔细检查并重新检查您的姓名。如果您在此处收到错误:ws1 = Excel.Workbooks("A.xlsx").Worksheets("sheet1")
请尝试隔离工作簿和工作表的分配,以找出导致错误的内容。
刚刚找到与Rory相同的东西;您必须将SET
用于工作簿,工作表或范围;或任何对象。
SET ws1 = Excel.Workbooks("A.xlsx").Worksheets("sheet1")