Increasing 'to' value as variable in For Next statement

  • Hello, I'm trying to increase the target number of the For Next statement in my code.


    E.g.


    Code
    Dim i, j As Integer
    j = 2
    
    
    
    
        For i = 1 To j + 1
            MsgBox i
            j = j + 1
        Next


    Obviously in this instance it would never end, but my actual code would have a target. Here the For Next statement takes the very first value (in this case 2 + 1 = 3) and keeps it regardless if i need to increases the loop by one more time (my actual code inbetween For Next will dictate), effectively leaving out additional essential 'run-throughs'.


    Might just be the case that it's not possible with this method. Any help appreciated.

  • Re: Increasing 'to' value as variable in For Next statement


    The upper bound of the For loop is evaluated once, at the beginning.


    Changing the value of j after the For line has executed will not make any difference to the number of iterations performed.

  • Re: Increasing 'to' value as variable in For Next statement


    How about some code like this:


  • Re: Increasing 'to' value as variable in For Next statement


    Quote from jdawson422;776039

    How about some code like this:




    Thanks for the responses. Yes, I've overcome it with a different loop method. Just takes a bit of creativity I guess.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!