Loop in macro doesn't work, call statement will - I don't understand

288enzo

Well-known Member
Joined
Feb 8, 2009
Messages
727
Office Version
  1. 2016
Platform
  1. Windows
I am actually going out of my mind with this. It works, then it doesn't.

The bit that is driving me crazy is
VBA Code:
Sub calc2()
    Dim modules As Long, lrow As Long
    modules = 6
    lrow = Sheets("sheet0").Range("A1").CurrentRegion.Rows.Count
    For a = 1 To modules
        Range("I2").Offset(, a).Value = _
            "=COUNTIFS(Sheet0!$H$4:$H$" & lrow & ",$F2,Sheet0!$A$4:$A$" & lrow & "," & Range("I1").Offset(, a).Address(external:=True) & ",Sheet0!$D$4:$D$" & lrow & ",""Completed"")" 'count if course is complete
    Next a
End Sub
Has_Has_Not_Taken_Report_04222022.xlsx.xls
JKLMNO
1619155M5B619155M6B619155M7B619156B619158BVCIWB01SSFIA22
2111101
Data
Cell Formulas
RangeFormula
J2J2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$J$1,Sheet0!$D$4:$D$6645,"Completed")
K2K2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$K$1,Sheet0!$D$4:$D$6645,"Completed")
L2L2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$L$1,Sheet0!$D$4:$D$6645,"Completed")
M2M2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$M$1,Sheet0!$D$4:$D$6645,"Completed")
N2N2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$N$1,Sheet0!$D$4:$D$6645,"Completed")
O2O2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$O$1,Sheet0!$D$4:$D$6645,"Completed")

When I run the above it works perfectly, when I incorporate it into the below, it doesn't.

VBA Code:
Sub macrotest()
    Dim modules As Long, lrow As Long, lcol As Long
    
    Sheets.Add.Name = "Data"
    Sheets("Sheet0").Range(Sheets("sheet0").Range("A4"), Sheets("sheet0").Range("A4").End(xlDown)).Copy Range("A1") 'copy list of course codes
        
    Range("A1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlNo 'remove duplicate course codes
    
    modules = Range("A1").CurrentRegion.Rows.Count
    
    Range("A1").CurrentRegion.Copy
    Range("J1").PasteSpecial , Transpose:=True 'move course codes across columns
    
    Range("J1").Offset(, modules).Value = "Total"

    lrow = Sheets("sheet0").Range("A1").CurrentRegion.Rows.Count 'get last row of sheet0 to copy data
    
    Range(Sheets("Sheet0").Range("N3"), Sheets("Sheet0").Range("O3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("A1")
    Range(Sheets("Sheet0").Range("Q3"), Sheets("Sheet0").Range("Q3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("C1")
    Range(Sheets("Sheet0").Range("L3"), Sheets("Sheet0").Range("M3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("D1")
    Range(Sheets("Sheet0").Range("H3"), Sheets("Sheet0").Range("H3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("F1")
    Range(Sheets("Sheet0").Range("F3"), Sheets("Sheet0").Range("G3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("G1")
    Range(Sheets("Sheet0").Range("K3"), Sheets("Sheet0").Range("K3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("I1")
    
    Range("A1").CurrentRegion.RemoveDuplicates Columns:=6, Header:=xlNo 'remove duplicates
    
    lrow = Range(Range("A1"), Range("A1").End(xlDown)).Count 'find last row after removing duplicates
    
    For a = 1 To modules
        Range("I2").Offset(, a).Value = _
            "=COUNTIFS(Sheet0!$H$4:$H$" & lrow & ",$F2,Sheet0!$A$4:$A$" & lrow & "," & Range("I1").Offset(, a).Address(external:=True) & ",Sheet0!$D$4:$D$" & lrow & ",""Completed"")" 'count if course is complete
    Next a
End Sub

Has_Has_Not_Taken_Report_04222022.xlsx.xls
JKLMNO
1619155M5B619155M6B619155M7B619156B619158BVCIWB01SSFIA22
2100000
Data
Cell Formulas
RangeFormula
J2J2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$J$1,Sheet0!$D$4:$D$1108,"Completed")
K2K2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$K$1,Sheet0!$D$4:$D$1108,"Completed")
L2L2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$L$1,Sheet0!$D$4:$D$1108,"Completed")
M2M2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$M$1,Sheet0!$D$4:$D$1108,"Completed")
N2N2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$N$1,Sheet0!$D$4:$D$1108,"Completed")
O2O2=COUNTIFS(Sheet0!$H$4:$H$1108,$F2,Sheet0!$A$4:$A$1108,Data!$O$1,Sheet0!$D$4:$D$1108,"Completed")


Now, this is where it gets interesting. If I add a call statement to my loop outside of the macro, it works. It's the same darn loop. I am beside myself.
VBA Code:
Sub macrotest()
    Dim modules As Long, lrow As Long, lcol As Long
    
    Sheets.Add.Name = "Data"
    Sheets("Sheet0").Range(Sheets("sheet0").Range("A4"), Sheets("sheet0").Range("A4").End(xlDown)).Copy Range("A1") 'copy list of course codes
        
    Range("A1").CurrentRegion.RemoveDuplicates Columns:=1, Header:=xlNo 'remove duplicate course codes
    
    modules = Range("A1").CurrentRegion.Rows.Count
    
    Range("A1").CurrentRegion.Copy
    Range("J1").PasteSpecial , Transpose:=True 'move course codes across columns
    
    Range("J1").Offset(, modules).Value = "Total"

    lrow = Sheets("sheet0").Range("A1").CurrentRegion.Rows.Count 'get last row of sheet0 to copy data
    
    Range(Sheets("Sheet0").Range("N3"), Sheets("Sheet0").Range("O3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("A1")
    Range(Sheets("Sheet0").Range("Q3"), Sheets("Sheet0").Range("Q3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("C1")
    Range(Sheets("Sheet0").Range("L3"), Sheets("Sheet0").Range("M3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("D1")
    Range(Sheets("Sheet0").Range("H3"), Sheets("Sheet0").Range("H3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("F1")
    Range(Sheets("Sheet0").Range("F3"), Sheets("Sheet0").Range("G3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("G1")
    Range(Sheets("Sheet0").Range("K3"), Sheets("Sheet0").Range("K3").Offset(lrow - 3).End(xlDown)).Copy Destination:=Range("I1")
    
    Range("A1").CurrentRegion.RemoveDuplicates Columns:=6, Header:=xlNo 'remove duplicates
    
    lrow = Range(Range("A1"), Range("A1").End(xlDown)).Count 'find last row after removing duplicates
    
    Call calc2
End Sub
Has_Has_Not_Taken_Report_04222022.xlsx.xls
JKLMNO
1619155M5B619155M6B619155M7B619156B619158BVCIWB01SSFIA22
2111101
Data
Cell Formulas
RangeFormula
J2J2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$J$1,Sheet0!$D$4:$D$6645,"Completed")
K2K2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$K$1,Sheet0!$D$4:$D$6645,"Completed")
L2L2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$L$1,Sheet0!$D$4:$D$6645,"Completed")
M2M2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$M$1,Sheet0!$D$4:$D$6645,"Completed")
N2N2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$N$1,Sheet0!$D$4:$D$6645,"Completed")
O2O2=COUNTIFS(Sheet0!$H$4:$H$6645,$F2,Sheet0!$A$4:$A$6645,Data!$O$1,Sheet0!$D$4:$D$6645,"Completed")
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
When you incorporate, I can see that you have 2 statements on lrow.

1) lrow = Sheets("sheet0").Range("A1").CurrentRegion.Rows.Count 'get last row of sheet0 to copy data
2) lrow = Range(Range("A1"), Range("A1").End(xlDown)).Count 'find last row after removing duplicates

As you can see in your formula, the lrow is different from the one that worked and the one that did not work. The lrow refers to 2 different sheets as I see it (unless I misunderstood ;))
 
Upvote 0
Solution
When you incorporate, I can see that you have 2 statements on lrow.

1) lrow = Sheets("sheet0").Range("A1").CurrentRegion.Rows.Count 'get last row of sheet0 to copy data
2) lrow = Range(Range("A1"), Range("A1").End(xlDown)).Count 'find last row after removing duplicates

As you can see in your formula, the lrow is different from the one that worked and the one that did not work. The lrow refers to 2 different sheets as I see it (unless I misunderstood ;))
Life savor!

I can't thank you enough. I removed the second lrow and all is good. Not sure why I had that in there in the first place.

I can sleep tonight.
 
Upvote 0

Forum statistics

Threads
1,224,903
Messages
6,181,653
Members
453,059
Latest member
jkevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top