Consulting

Results 1 to 2 of 2

Thread: Possible to for loop through 2 ranges?

  1. #1
    VBAX Mentor Movian's Avatar
    Joined
    Aug 2008
    Location
    NC, USA
    Posts
    399

    Possible to for loop through 2 ranges?

    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)
    "From the ashes of disaster grow the roses of success" - Chitty chitty bang bang

    "I fear not the man who has 10,000 kicks practiced once. I fear the man who has 1 kick practiced 10,000 times" - Bruce Lee

  2. #2
    VBAX Expert
    Joined
    Oct 2012
    Posts
    726
    Location
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •