Consulting

Results 1 to 5 of 5

Thread: Options List for Inputbox

  1. #1
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Options List for Inputbox

    How can one provide a "data validation, list" functionality for an InputBox?

    I want to limit the options in an InputBox to only the following values 1,2,3,4,5.

    Regards,
    vanhunk

  2. #2
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I would think it much easier to use a textbox on a userform and probably the keypress and change events.

  3. #3
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    afaik, you can't.

    but you can specify the type using Application.Inputbox:
    https://msdn.microsoft.com/en-us/lib.../ff839468.aspx

    you should test the inputbox result to see if it meets the condition.
    if not, alert user by displaying related messages...

    an example from http://www.ozgrid.com/forum/showthread.php?t=54032 post#5

    Sub GetText()
    
        Dim strReply As String 
         
        strReply = Application.InputBox(Prompt:="Enter Text", Type:=2) 
        If strReply = "False" Then Exit Sub 
         
        If Len(strReply) > 5 Or Len(strReply) < 4 Then 
            MsgBox "Text must be 5 characters long" 
            Run "GetText" 
        ElseIf IsNumeric(Left(strReply, 1)) Then 
            MsgBox "First character must be a letter" 
            Run "GetText" 
        ElseIf Not IsNumeric(Mid(strReply, 2, 4)) Then 
            MsgBox "last characters must be numeric" 
            Run "GetText" 
        End If
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    Sub M_snb()
      Do
        y = InputBox("number 1,2,3,4 or 5", "snb")
      Loop Until y > 0 And y < 6
    End Sub

  5. #5
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    240
    Location

    Thumbs up

    Thank you @GTO, @mancubus, and @snb:

    I found the code below and adapted it for my application, but I very much prefers snb's concise and simple approach.

    Sub Test()
        'Const var As String = "var"
        Dim projects As Variant
        Dim x As Long
        'projects = Array("x", "var", "y")
        projects = Array("1", "2", "3")
        On Error Resume Next
        x = WorksheetFunction.Match(4, projects, False)
        If Err = 0 Then
    '       **** do something ****
            MsgBox "Found"
        Else
    '       **** clear error ****
            Err.Clear
            MsgBox "Not Found"
        End If
        On Error GoTo 0
    End Sub
    Best Regards,
    vanhunk

Tags for this Thread

Posting Permissions

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