我知道我没有正确执行此操作,但我是 VBA 初学者,并且正在尝试在工作簿中的多个工作表上执行值的 sumif,然后返回这些 sumif 的总和。
这就是我尝试的方式,但我得到了#NAME?错误,不知道从这里去哪里。
Public Function Lookup(x As Long) As Long
Dim sh As Worksheet, wb As Workbook, arr() As Variant
Set wb = Workbooks.Open("Purchased.xlsm")
For Each sh In wb
arr() = Application.SumIf(Range("A1:A10000"), x, Range("B1:B10000"))
Next
Lookup = Application.WorksheetFunction.Sum(arr())
End Function
我一直无法让这个工作,它返回一个#VALUE!错误,但我认为这可能更接近我需要的:
Public Function AddItUp(x As Long) As Long
Dim sh As Worksheet, wb As Workbook, RunningSum() As Variant
Dim i As Long, j As Long
Set wb = Workbooks.Open("Week1Input.xlsm")
j = wb.Worksheets.Count
ReDim RunningSum(1 To j)
For i = 1 To j
RunningSum(i) = Application.SumIf(wb.sh(i).Range("A1:A10"), x, wb.sh(i).Range("B1:B10"))
Next i
AddItUp = Application.WorksheetFunction.Sum(RunningSum)
End Function