For Each loop issue

VeryForgetful

Board Regular
Joined
Mar 1, 2015
Messages
242
Hi all,

I have an issue with a for each loop where I want to limit a set range to processing 100 rows on each run.

So the first interaction would be range L2 to L101 then L102 to L203 and so on. The last one would more than likely have less than 100 rows so that would only need to loop as far as the last row.

Any ideas please?
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Can you post what you already have ???
 
Upvote 0
Thanks

Code:
[COLOR=#454545][FONT=&quot][FONT=&quot]Sub Example()[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot][/FONT]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]Dim cel As Range, rng As Range, lr as long[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]Dim MyStr as String[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot][/FONT]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]lr = Cells(Rows.Count, “L”).End(xlUp).row[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]Set rng = Range(“L2:L” & lr)[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot][/FONT]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]For Each cel In rng[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]  MyStr = MyStr & cel.Value & “,”[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]Next cel[/FONT][/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot][/FONT]
[/FONT][/COLOR]
[COLOR=#454545][FONT=&quot][FONT=&quot]End Sub[/FONT][/FONT][/COLOR]
 
Upvote 0
Not sure what you are trying to do but perhaps you could use something like this.
Code:
Sub Example()
Dim cel As Range, rng As Range, lr as long
Dim MyStr as String
Dim cnt As Long

    lr = Cells(Rows.Count, “L”).End(xlUp).row
    Set rng = Range(“L2:L” & lr)

    For Each cel In rng
        MyStr = MyStr & cel.Value & “,”
        cnt = cnt + 1
        If cnt = 100 Then
            Debug.Print MyStr
            MyStr = ""
            cnt = cnt + 1
        End If
    Next cel

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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