+ Reply to Thread
Results 1 to 5 of 5

VBA For loop where number of iterations can be changed from inside loop

  1. #1
    Registered User
    Join Date
    08-18-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    VBA For loop where number of iterations can be changed from inside loop

    Hi Guys,

    I have some code that is like this:

    For i = 1 To Num
    If criteria
    do some stuf
    Num = Num + 1
    End if
    Next i

    So basically, each time the condition is true, along with doing all the stuff, i also want it to run an extra time.

    I though the above code would be fine, but say Num is originally 20, it will still only run to 20 even when Num now = 24 after being modified from within the loop.

    Any clues as to how I could fix this?

    Thanks in advance.

  2. #2
    Forum Expert davegugg's Avatar
    Join Date
    12-18-2008
    Location
    WI, US
    MS-Off Ver
    2010
    Posts
    1,884

    Re: VBA For loop where number of iterations can be changed from inside loop

    Its hard to know without knowing what "do some stuff" does, but you could try:

    1. Using a do until - loop instead of a for loop.
    2. Instead of the line (or in addition to it) Num = Num + 1 use i = i-1.
    Is your code running too slowly?
    Does your workbook or database have a bunch of duplicate pieces of data?
    Have a look at this article to learn the best ways to set up your projects.
    It will save both time and effort in the long run!


    Dave

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,445

    Re: VBA For loop where number of iterations can be changed from inside loop

    Perhaps you could explain what it is you are trying to achieve

    Regards
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Registered User
    Join Date
    08-18-2010
    Location
    London, England
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: VBA For loop where number of iterations can be changed from inside loop

    The Do Until thing worked, thanks a lot. Basically, if you change the x in "For i = 1 to x" from inside the loop it doesn't actually run the loop the new amount of times. But I did:
    Do Until i = Num

    i = i + 1
    Loop

    And this now works, I can increase Num within the loop and it will keep running until it reaches the increased Num, which the For loop wasn't able to do.

    Thanks for your help.

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: VBA For loop where number of iterations can be changed from inside loop

    It's a cardinal sin of programming to change the loop counter inside a For/Next loop, and the loop limits are only evaluated once because they may be complex expressions. You're on the right track with a Do/Loop as Dave suggested.
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1