Loop Input Box if Entry is Invalid

temerson

New Member
Joined
Apr 22, 2019
Messages
39
I am wanting to restrict the user entering into the inputbox to one of four answers.
If not, the inputbox will loop until one of four answers is given.

The answer would include both text and a number, a1, a2, b1 or b2. Would declaring the variable as a string be appropriate?

I figured I could do this by using Do While or Loop While but I have no idea where to edit my code.

sub VendorType
dim vendor as string

distro = InputBox("Vendor Type", "Vendor Distribution Type")

If distro = "A1" Then


Thank you in advance!
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Try this

VBA Code:
Sub VendorType()
  Dim distro  As String
  Do While True
    distro = InputBox("Vendor Type (a1, a2, b1 or b2)", "Vendor Distribution Type")
    If StrPtr(distro) = 0 Then
      MsgBox "Click Cancel, exit now"
      Exit Sub
    ElseIf Len(distro) = 0 Then
      MsgBox "Please enter Type: a1, a2, b1 or b2"
    Else
      Select Case LCase(distro)
        Case "a1", "a2", "b1", "b2"
          Exit Do
        Case Else
          MsgBox "Please enter Type: a1, a2, b1 or b2"
      End Select
    End If
  Loop
  '
  '
  'continue your code
  '
  '
End Sub
 
Upvote 0
Hi,
try

VBA Code:
Sub VendorType()
    Dim distro  As String, Prompt As String
    Dim arr As Variant, m As Variant
   
    arr = Array("a1", "a2", "b1", "b2")
    Prompt = "Enter Vendor Type" & Chr(10) & "A1, A2, B1 or B2 only"
    Do
        distro = InputBox(Prompt, "Vendor Distribution Type")
'cancel pressed
        If StrPtr(distro) = 0 Then Exit Sub
        m = Application.Match(distro, arr, 0)
        If Not IsError(m) Then Exit Do
        MsgBox Prompt, 48, "Invalid Entry"
    Loop
   
   
'rest of code e.g
    Select Case UCase(distro)
       
    Case "A1"
       
    Case "A2"
       
    Case "B1"
       
    Case "B2"
       
    End Select
   
   
End Sub

Dave
 
Last edited:
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,954
Messages
6,122,461
Members
449,085
Latest member
ExcelError

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top