Consolidate Multiple Loop Statements

spartanjim

New Member
Joined
Jan 29, 2012
Messages
2
I'm working in XL07 on Win7.

The workbook has two worksheets "Data" & "Crunch". "Data" contains the raw data-range B1:U50. "Crunch" is the processing worksheet. Row 1 is a header row. Columns A-D are empty. Column E (rows 2-81) contain the numbers 1-80.

The macro is intended to run in "Crunch". I need to add the CountIf function in every cell/row filling the range F2:BC81. A loop does the trick for me to fill the rows in each column but I don't know how to move the loop down each column without creating a new loop.

A section of the code is below. Notice a majority of the code is the same, the copy in red is variable from loop to loop. I need to repeat this loop through column "BC" (50 times in total). My guess is there is a way to write this code one time instead of 50 times adjusting the variable components.

Code:
'Find total # of records and then store in variable
    totalrecords = ActiveSheet.UsedRange.Rows.Count
'CountIf Statement
    For Row = totalrecords To 2 Step -1
        If Cells(Row, 5).Value <> "0" Then
            Cells(Row, [COLOR="Red"]6[/COLOR]).Value = "=COUNTIF(Data!$B$1:$U$[COLOR="red"]1[/COLOR], E" & Row & ")"
        End If
    Next Row
    
'Find total # of records and then store in variable
    totalrecords = ActiveSheet.UsedRange.Rows.Count
'CountIf Statement
    For Row = totalrecords To 2 Step -1
        If Cells(Row, 5).Value <> "0" Then
            Cells(Row, [COLOR="red"]7[/COLOR]).Value = "=COUNTIF(Data!$B$1:$U$[COLOR="red"]2[/COLOR], E" & Row & ")"
        End If
    Next Row

'Find total # of records and then store in variable
    totalrecords = ActiveSheet.UsedRange.Rows.Count
'CountIf Statement
    For Row = totalrecords To 2 Step -1
        If Cells(Row, 5).Value <> "0" Then
            Cells(Row, [COLOR="red"]8[/COLOR]).Value = "=COUNTIF(Data!$B$1:$U$[COLOR="red"]3[/COLOR], E" & Row & ")"
        End If
    Next Row

'Find total # of records and then store in variable
    totalrecords = ActiveSheet.UsedRange.Rows.Count
'CountIf Statement
    For Row = totalrecords To 2 Step -1
        If Cells(Row, 5).Value <> "0" Then
            Cells(Row, [COLOR="red"]9[/COLOR]).Value = "=COUNTIF(Data!$B$1:$U$[COLOR="red"]4[/COLOR], E" & Row & ")"
        End If
    Next Row



Thank you
Jim
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Welcome to the Board!

You can count the number of columns just like you do rows.

Dim LC as Long
LC = Cells(1,Columns.Count).End(xltoLeft).Column

Then you can add another loop inside the row loop:

For x = 1 to LC
Cells(Row, x)...

HTH,
 
Upvote 0
Smitty, thank you for the quick response!

I'm not very good with excel and have no programming background but your lead was enough to clear my confusion.

Your lead, followed by finding this thread got me over the hump.

Here's the code I ended up using:

Code:
'Find the last filled row in column E
    LR = Cells(Rows.Count, 5).End(xlUp).Row
'Find the last filled column in row 1
    LC = Cells(1, Columns.Count).End(xlToLeft).Column
'CountIf Statement
    For Row = LR To 2 Step -1
    For Col = LC To 6 Step -1
        Cells(Row, Col).Value = "=COUNTIF(Data!$B$1:$U" & Col - 5 & ", E" & Row & ")"
    Next Col
    Next Row

Again, thank you!
Jim
 
Upvote 0

Forum statistics

Threads
1,224,218
Messages
6,177,198
Members
452,764
Latest member
Mark1963

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