我试图设置一个VBA循环函数,它将遍历许多特定的工作表,在每个工作表中执行一个嵌套循环。嵌套循环是一个sumif函数,我需要将它存储在sumHACC
中,直到每个工作表循环完成,最终值(每个sumif的总和)将输入到单个单元格中。
嵌套循环在不同的if条件下单独使用(参见底部的'ALL OTHER FUNDERS
下面),但是我努力将其嵌套在循环中以在不同的工作表中迭代它 - 双循环在'IF HACC IS SELECTED
中。需要循环播放的每个工作表的名称存储在单元格D6:D19
中,我将其引入HACCRange
,并且我尝试使用以下代码循环遍历这些工作表:
For Each HACC In HACCRange
Set calcTab1 = Sheets(HACC)
这是我收到类型不匹配错误的地方。本节中的代码并不完整(即没有更新到sumHACC),因为我完全了解如何使这项工作成功!
技术上还有另一个循环(sumif条件向下移动一个列表(参考)),但这似乎不是问题所在。非常感谢任何帮助!
Sub FunderLevel()
Dim reference As Range
Dim Funder As String
Dim itemRef1 As Range
Dim itemRef3 As Range
Dim calcTab1 As Worksheet
Dim calcTab3 As Worksheet
Dim sumCol As Range
Dim printCalc As Range
Dim HACCRange As Range
Dim QCCRange As Range
i = 21
Set reference = Range("A21:A22")
Funder = Range("A10")
'IF HACC IS SELECTED
If Funder = "HACC" Then
sumHACC = 0
Set HACCRange = Sheets("Reference Sheet").Range("D6:D19")
For Each HACC In HACCRange
Set calcTab1 = Sheets(HACC)
Set itemRef1 = calcTab1.Range("A10:A500")
myCol = calcTab1.Rows(7).Find(What:="All", LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
With calcTab1
Set sumCol = calcTab1.Range(.Cells(10, myCol), .Cells(500, myCol))
End With
For Each Cell In reference
Set printCalc = Cells(i, 2)
printCalc = WorksheetFunction.SumIf(itemRef1, Cell, sumCol)
i = i + 1
Next Cell
Next HACC
'ALL OTHER FUNDERS
Else:
Set calcTab3 = Sheets(Funder)
Set itemRef3 = calcTab3.Range("A10:A500")
myCol = calcTab3.Rows(7).Find(What:="All", LookIn:=xlValues, LookAt:=xlWhole, _
SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Colum
With calcTab3
Set sumCol = calcTab3.Range(.Cells(10, myCol), .Cells(500, myCol))
End With
For Each Cell In reference
Set printCalc = Cells(i, 2)
printCalc = WorksheetFunction.SumIf(itemRef3, Cell, sumCol)
i = i + 1
Next Cell
End If
End Sub
答案 0 :(得分:2)
试试这个:
For Each HACC In HACCRange.Cells
Set calcTab1 = Sheets(HACC.value)