Input box prompt with only 5 possible entries

Dear Experts:

Could somebody please help me with a code snippet for an Input Box:

The user is to be prompted with an inputbox that only allows 5 entries: DE or EN or ES or FR or IT. Only capital letters are allowed.

Should the user enter something other than DE, EN, ES, FR or IT, the macro is not to exit but the inputbox is to reappear.

Help is much appreciated. Thank you very much in advance.

Regards, Andreas
Andreas HermleTeam leaderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike in ITIT System AdministratorCommented:
Can you provide a workbook example of what you have? Or at least the code that you already have?
0
Roy CoxGroup Finance ManagerCommented:
Do you mean

Option Explicit

Sub TestInput()
Dim vInput
    
 
start_again:
    vInput = InputBox("Enter DE, EN, ES, FR or IT", "Thanks")
    If Len(vInput) = 0 Then
        MsgBox "Nothing Entered"
        GoTo start_again
    Else
    Select Case vInput
    Case "DE", "EN", "ES", "FR", "IT"
        MsgBox "Input " & vInput & " correct"
        Case Else
        MsgBox "Input " & vInput & " incorrect"
        GoTo start_again
        End Select
    End If
    
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
In my opinion the most elegant way to this would be to create a UserForm with either a listbox or a drop-down style combobox limited to your valid values.

If you prefer not to do that, I would use a slight tweak to the code Roy_Cox provides above.  The differences: it shows how to make the code case-insensitive (by default Excel VBA is case sensitive in text comparisons), and how to void using GoTo.

Option Explicit

Sub LimitedInput()
    
    Dim InputOK As Boolean
    Dim GetInput As String
    
    InputOK = False
    
    Do Until InputOK
        GetInput = InputBox("Enter DE, EN, ES, FR or IT", "Thanks")
        ' if you want it to be case-sensitive omit the next line
        GetInput = UCase(GetInput)
        Select Case GetInput
            Case "DE", "EN", "ES", "FR", "IT"
                InputOK = True
            Case Else
                MsgBox "Try again, dummy", vbCritical, "Invalid Entry"
        End Select
    Loop
    
End Sub

Open in new window


Another way to make it case-insensitive:

Option Explicit
Option Compare Text

Sub LimitedInput()
    
    Dim InputOK As Boolean
    Dim GetInput As String
    
    InputOK = False
    
    Do Until InputOK
        GetInput = InputBox("Enter DE, EN, ES, FR or IT", "Thanks")
        Select Case GetInput
            Case "DE", "EN", "ES", "FR", "IT"
                InputOK = True
            Case Else
                MsgBox "Try again, dummy", vbCritical, "Invalid Entry"
        End Select
    Loop
    
End Sub

Open in new window

0
Roy CoxGroup Finance ManagerCommented:
I would myself use an UserForm but Andreas wanted an InputBox.
0
Roy CoxGroup Finance ManagerCommented:
Pleased to help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.