Results 1 to 3 of 3

Thread: Loop until valid response

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2017
    Posts
    7

    Loop until valid response

    I would like to validate response to an InputBox, and repeat the prompt if invalid. Below is a simple example. It seems that I could use a True/False boolean variable with a loop to do this. I would prefer to not have to set a "True" value within every valid response case, as there could be lots of valid cases, and that seems inefficient. Would like to just set something within the "Else" case to cause it to loop the prompt code until valid. But I can't seem to think how to do that. :-( (Maybe just a brain f**t, or senior moment?) Thanks for any help! (I am using VBA 7.1 in Excel, but don't think that would matter?)

    Code:
    Response = InputBox(myMsg)
    Select Case Response
     Case "A"
      'do A stuff
     Case "B"
      'do B stuff
     Case "C"
      'do C stuff
     Case "D"
      'do D stuff
     Case Else
      'prompt again
    End Select
    Last edited by dday9; Aug 9th, 2017 at 08:25 AM. Reason: Added Code Tags

  2. #2
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,398

    Re: Loop until valid response

    Maybe something like this:

    Code:
    blnResponseValid = False
    Do While blnResponseValid = False
    Response = InputBox(myMsg)
    blnResponseValid = True
     Select Case Response
     Case "A"
     'do A stuff
     Case "B"
     'do B stuff
     Case "C"
     'do C stuff
     Case "D"
     'do D stuff
     Case Else
     'prompt again
     blnResponseValid = False
     End Select 
    Loop

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2017
    Posts
    7

    Re: Loop until valid response

    Duh!! I took a nap, and now it makes sense! :-( I was thinking that resetting to True within the loop would override setting it False in the "Else" case. But obviously (to most people!), the "Do While" would check the flag first and repeat the loop. Sorry to bother you!

Posting Permissions

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



Click Here to Expand Forum to Full Width