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 do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
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

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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