+ Reply to Thread
Results 1 to 6 of 6

VBA infinite loop

  1. #1
    Forum Contributor
    Join Date
    01-06-2011
    Location
    cleveland
    MS-Off Ver
    Excel 2007
    Posts
    225

    VBA infinite loop

    Please Login or Register  to view this content.
    when i hit F5 the excel program just keeps processing and i terminate in the editor by escape key. This is the first loop that I have tried and the guide that I am following has set up their example for the "Do Until" loop differently than I and I cannot apply that method.

    what i have done is I have counted all the cells that contain a special character. the cell below that value is put into another worksheet in a running list, and the cell in worksheet "user" that has the special character is deleted. I did a countif of these special characters as a way of letting the code know how many more times it will need to loop but this is obviously not working. is it that the function within the cell does not update between loops and when the loop occurs it still reads the initial value upon looping, thus the infinite loop?

    in your experiences, what have you found to be the best way to achieve simple loops?

  2. #2
    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
    48,470

    Re: VBA infinite loop

    You set Loopsleft to the value in cell B8 but you don't decrement it in the loop.

    Please Login or Register  to view this content.
    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


  3. #3
    Forum Contributor
    Join Date
    01-06-2011
    Location
    cleveland
    MS-Off Ver
    Excel 2007
    Posts
    225

    Re: VBA infinite loop

    Quote Originally Posted by TMShucks View Post
    You set Loopsleft to the value in cell B8 but you don't decrement it in the loop.

    Please Login or Register  to view this content.
    Regards
    the mathematical side in me is finding this statement very amusing. thank you for your patience and help I think it is starting to come together in my mind. this is rather intuitive

  4. #4
    Valued Forum Contributor StevenM's Avatar
    Join Date
    03-23-2008
    Location
    New Lenox, IL USA
    MS-Off Ver
    2007
    Posts
    910

    Re: VBA infinite loop

    It depends on what you are trying to do.

    Please Login or Register  to view this content.

  5. #5
    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
    48,470

    Re: VBA infinite loop

    You're welcome. Thanks for the rep.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA infinite loop

    Hi

    In general if you know the number of times something must be done then you should use a For..Next loop.

    A Do..Loop (or its variations Do Until..Loop, Do While...Loop, Do..Loop Until, Do...Loop While) is only used when you don't know precisely when something should be terminated.

    Your loop variable 'LoopsLeft' never changes from its B8 value, hence the loop never terminates.

    Without seeing your workbook its difficult to comment much further. But are you sure you should be using a loop at all. Have you considered a helper column in your data which identifies the special character in the row below, then filter your data on the helper column before using that filtered data.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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