我有一个功能,可以在工作表中查找值并将这些值相加。只要所查找的值存在于所有表中,它就起作用。如果该值不存在,我只想将结果值设置为0。
Sub lookupSum3()
Dim myVlookupResult As Double
Dim myTableArray As Range
Dim myVlookupSum As Double
Dim i As Integer
Dim sheetCount As Integer
Dim ws As Worksheet
Dim ws1 As Worksheet
Dim j As Integer
Dim rowCount As Integer
Set ws1 = Sheets(1)
sheetCount = Sheets.count
rowCount = ws1.Range("A1", ws1.Range("A1").End(xlDown).End(xlDown).End(xlUp)).Rows.count
i = 2
j = 2
Do While j <= rowCount
Do While i <= sheetCount
Set ws = Sheets(i)
Set myTableArray = ws.Range("A:N")
myVlookupResult = Application.vlookup(ws1.Range("A" & j), myTableArray, 5, False)
If IsError(myVlookupResult) = True Then
myVlookupResult = 0
End If
myVlookupSum = myVlookupSum + myVlookupResult
i = i + 1
Loop
i = 2
ws1.Range("B" & j) = myVlookupSum
myVlookupSum = 0
j = j + 1
Loop
MsgBox rowCount
End Sub
代码将在第myVlookupResult = Application.vlookup(ws1.Range("A" & j), myTableArray, 5, False)
行显示运行时错误'13'
我不正确地处理错误吗?