+ Reply to Thread
Results 1 to 5 of 5

Loop back to start of If from Else statement

  1. #1
    Registered User
    Join Date
    05-12-2016
    Location
    NZ
    MS-Off Ver
    Office 14
    Posts
    41

    Loop back to start of If from Else statement

    Hi there,

    Is there way to loop back to the top of the If statement from the Else statement?

    I'm validating a string (from InputBox) against dozens of conditions, so I've got code through each valid condition using InStr. If the string doesn't match any, it hits the Else statement, which prompts another InputBox linked to the same string saying "You entered an invalid value, please carefully enter... etc." and then the user can try again to enter a correct value. If this condition is triggered, I want to move through the list of conditions again by going back to the top of the If statement and going through again.

    Sample code:

    Please Login or Register  to view this content.

    Elsewhere I've seen people using While statements, but since I'm checking against 26 possible values it seems messy to use "While InStr(MyString, "Bird") = 0 Or InStr(MyString, "Dog") = 0 Or....." etc.

    Is there an easier way?
    Thanks,
    Pango

  2. #2
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,915

    Re: Loop back to start of If from Else statement

    Try this ...
    Please Login or Register  to view this content.

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Loop back to start of If from Else statement

    Pango

    You don't need to have every condition in the While, use a boolean value to indicate if a valid entry has been made.

    That's what I've done in the code below and I've also provided a way to exit the loop by pressing Cancel on the 2nd inputbox.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    05-12-2016
    Location
    NZ
    MS-Off Ver
    Office 14
    Posts
    41

    Re: Loop back to start of If from Else statement

    Thank you both so much - they both do the job perfectly. I can't believe I forgot about GoTo, so I'll use that because it takes up less space in an already very lengthy process.


    Thank you!

  5. #5
    Forum Guru
    Join Date
    02-27-2016
    Location
    Vietnam
    MS-Off Ver
    2016
    Posts
    5,915

    Re: Loop back to start of If from Else statement

    You're welcome!

+ 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. For Loop, jumpoing back all of a sudden
    By Excel_vba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-19-2013, 09:23 AM
  2. When sum is 10 or more start back at 0
    By jrzone2k in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-22-2013, 01:29 AM
  3. [SOLVED] Loop till end of the sheet and loop back to first column again
    By smlim7 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-29-2012, 10:03 AM
  4. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  5. VBA for pushing back start date
    By lovethepirk in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-09-2011, 12:31 PM
  6. Push back start date
    By lovethepirk in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-11-2011, 07:16 PM
  7. Moving from For...Next loop to subroutine and back to loop
    By zabrahamson in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-25-2011, 01:26 PM
  8. Tab back to start
    By BuzzT in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-30-2010, 02:59 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