Input box prompt with only 5 possible entries

Andreas Hermle
Andreas Hermle used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Mike in ITIT System Administrator
Distinguished Expert 2017

Commented:
Can you provide a workbook example of what you have? Or at least the code that you already have?
Group Finance Manager
Commented:
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

Top Expert 2010
Commented:
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

Roy CoxGroup Finance Manager

Commented:
I would myself use an UserForm but Andreas wanted an InputBox.
Roy CoxGroup Finance Manager

Commented:
Pleased to help

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial