+ Reply to Thread
Results 1 to 4 of 4

For Loop with If statement and exit sub followed by else

  1. #1
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    For Loop with If statement and exit sub followed by else

    I need a code that is similar to "IF" function but in the "for loop" :

    Normally that would be :

    Please Login or Register  to view this content.

    But i need combine it in the loop

    Please Login or Register  to view this content.
    When i do this way it does not do "ELSE" but only exits sub

    And even if conditions are not met, the function still exits sub instead of carrying on rest of the code..

  2. #2
    Forum Expert
    Join Date
    02-22-2013
    Location
    London, UK
    MS-Off Ver
    Office 365
    Posts
    1,218

    Re: For Loop with If statement and exit sub followed by else

    Hi,

    you need to break the line
    Please Login or Register  to view this content.
    and write something like
    Please Login or Register  to view this content.
    or everything on one line,
    Please Login or Register  to view this content.
    See how that works

  3. #3
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: For Loop with If statement and exit sub followed by else

    Quote Originally Posted by berlan View Post
    Hi,

    you need to break the line
    Please Login or Register  to view this content.
    and write something like
    [CODE]
    For i = 1 To 20

    IF <Condition1> and <Condition2> then
    MsgBox ("Not met")
    Exit Sub
    Else

    '"DO STUFF"

    End If

    ...

    See how that works
    I already tried this but there is an error saying : "For without Next" and when I add "next" it says: "Next without if"

  4. #4
    Forum Contributor
    Join Date
    02-23-2015
    Location
    united kingdom
    MS-Off Ver
    14.0.7143.5000 (32-bit)
    Posts
    124

    Re: For Loop with If statement and exit sub followed by else

    Quote Originally Posted by berlan View Post
    Hi,

    you need to break the line
    Please Login or Register  to view this content.
    and write something like
    Please Login or Register  to view this content.
    or everything on one line,
    Please Login or Register  to view this content.
    See how that works
    I worked it out. Thanks for help.
    It should be in following order
    Please Login or Register  to view this content.

+ 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] For Next Loop Does Not Exit
    By goss in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2014, 04:52 PM
  3. [SOLVED] How to re-use macro? Best way to loop and "exit" with comparison statement ?
    By varreddor in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-21-2013, 08:43 PM
  4. ESCAPE ket to exit loop??
    By nikko50 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-25-2013, 07:49 PM
  5. Exit loop
    By ashmott in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-19-2011, 05:41 AM
  6. exit loop when find value?
    By sweetboy02125 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2011, 03:42 PM
  7. [SOLVED] exit for next loop
    By David Henderson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-23-2006, 02: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