PDA

View Full Version : Possible to for loop through 2 ranges?



Movian
03-01-2016, 06:54 AM
I am wondering if there is any way to loop through two sets of ranges in a single for loop.

E.G.


For counter = 1 to 10 THEN 20 to 25
msgbox counter
next

Is there a way to do this? or do I need to just write a second for loop (trying to make the code easy to read and avoid redundancy here)

jonh
03-01-2016, 09:20 AM
Ex3 would be my preferred method.


Sub ex1()
For counter = 1 To 25
If counter = 11 Then counter = 20
Debug.Print counter
Next
End Sub


Sub ex2()
For counter = 1 To 25
Select Case counter
Case 1 To 10, 20 To 25
Debug.Print counter
End Select
Next
End Sub


Sub ex3()
main 1, 10
main 20, 25
End Sub

Sub main(startat, endat)
For counter = startat To endat
Debug.Print counter
Next
End Sub