MrExcel Publishing
Your One Stop for Excel Tips & Solutions

nesting loops in macros


Posted by SUZ on August 10, 2001 8:05 AM

Are nested loops possible in macros? Can anyone show me an example? I'm having trouble understanding where to put my "SUB" and "END SUB."


Posted by Barrie Davidson on August 10, 2001 8:10 AM

Yes they are. An example would be:

Sub Example()
Range("A1").Select
Do Until Selection.Row > 10
Selection.Offset(1, 0).Select
Loop
End Sub

This macro goes to A1 and then goes down one row at a time until the row number is greater than 10.

Hope this helps you out.

Barrie

Posted by SUZ on August 10, 2001 8:33 AM

Ok, I get that part, but what if for each of those rows, I need to run ANOTHER procedure 5 times? Can I put a loop inside of that loop, and if so, how?

Thanks for your help.

Suz

Posted by Barrie Davidson on August 10, 2001 8:39 AM

Yes, you can put a loop inside a loop. Here's how you'd change my example (I'm assuming you would use some kind of counter):

Sub Example()
Dim Counter As Integer
Range("A1").Select
Do Until Selection.Row > 10
Selection.Offset(1, 0).Select
Counter=0
Do Until Counter=5
'YOUR PROCEDURE
Counter=Counter + 1
Loop
Loop
End Sub

Barrie