简体   繁体   中英

Excel VBA loop through specific sheets with subloop

I'm trying to set up a VBA loop function that will iterate through a number of specific sheets, conducting a single nested-loop within each sheet. The nested loop is a sumif function, and I need to store this in sumHACC until each sheet has been looped through, with the final value (sum of each sumif) to be entered into a single cell.

The nested loop is being used on its own under a different if condition (see under 'ALL OTHER FUNDERS at the bottom), but I'm struggling to nest this within the loop to iterate it across different sheets - The double loop is in 'IF HACC IS SELECTED . The name of each sheet that needs to be looped through is stored in cells D6:D19 , which I pull into HACCRange , and I'm trying to loop through these sheets with:

For Each HACC In HACCRange
Set calcTab1 = Sheets(HACC)

and this is where I receive a Type mismatch error. The code in this section isn't complete (ie no update to sumHACC) as I'm completely bamboozled as to how to make this work!

There is technically another loop on top of this (the sumif condition moves down a list (reference)), however this doesn't seem to be the problem. Any assistance would be greatly appreciated!

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

Try this:

For Each HACC In HACCRange.Cells
   Set calcTab1 = Sheets(HACC.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM