+ Reply to Thread
Results 1 to 4 of 4

How to make a graceful exit from FOR TO Statement with nested IF THEN

  1. #1
    Registered User
    Join Date
    10-01-2008
    Location
    Park City
    Posts
    59

    How to make a graceful exit from FOR TO Statement with nested IF THEN

    I have the following code and I keep getting an error on the if then exit


    Please Login or Register  to view this content.
    I get a "complie error: Next without For" at the "Else Next i" line.

    I don't really understand why? The syntax seems like it should work.

    Anyway I fixed it with:

    Please Login or Register  to view this content.

    Is there a more graceful way to exit this loop? What I'm trying to say is that if Z = 0 start at the next i.


    Thanks,

    GG

  2. #2
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: How to make a graceful exit from FOR TO Statement with nested IF THEN

    Please Login or Register  to view this content.
    I suppose, I would normally try and define where variable1 becomes 0 and set that to the value of Z before entering the loop though, makes it easier to follow/debug the code in the long run.

    Edit:

    Or use a do.. until construct like Miraun suggests.
    Last edited by ragulduy; 06-25-2014 at 11:46 AM.

  3. #3
    Valued Forum Contributor Miraun's Avatar
    Join Date
    04-03-2009
    Location
    New England
    MS-Off Ver
    2003, 2007, 2010, 2013
    Posts
    554

    Re: How to make a graceful exit from FOR TO Statement with nested IF THEN

    Excel really hates variables being messed when using for i = x to y commands. I could try to go into a technical reason... but the fact remains, I haven't had much luck altering i,x,y variables within the for/next commands.

    If you're going to be manipulating the variables on the fly with subsequent subroutines, you're better off using a Do until i > y / loop command. It gives you more control over the variables to be dynamically altered based on subsequent changes.
    Going for Guru! Click the Star to the bottom left of this post if I helped!

  4. #4
    Registered User
    Join Date
    10-01-2008
    Location
    Park City
    Posts
    59

    Re: How to make a graceful exit from FOR TO Statement with nested IF THEN

    The "Exit For" command is what I was looking for. Much more graceful than altering the variables on the fly.

    Thanks for the advice.


    GG

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. How to exit a function and proceed to the Next statement of a For Loop
    By sugaprasad in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-07-2014, 10:41 AM
  2. [SOLVED] Make 7 digit number on exit textbox
    By vijaynadiad in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-19-2013, 02:32 PM
  3. [SOLVED] Nested Loops and Exit For Statement
    By RGrunden in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-18-2012, 02:26 PM
  4. Replies: 6
    Last Post: 01-14-2009, 06:59 PM
  5. [SOLVED] How do I make Command button ( Save & Exit) for Excel
    By Abutaha in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-16-2005, 06:45 PM

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