InputBox Excel Add-in VBA (Visual Basic for Applications) Example

InputBox Excel Add-in VBA (Visual Basic for Applications) Example

InputBox

Inputbox is a user interface control which is used to take inputs from end user. It can be customized in different ways based on need. In this article we will see how we can prompt user with InputBox and take inputs which we can process in our code:

Example

Syntax

InputBox (Prompt, Title, Default, Left, Top, HelpFile, HelpContextID, Type)

Parameters

  • Prompt (Required): The message to be displayed in the dialog box. This can be a string, a number, a date, or a Boolean value (Microsoft Excel automatically converts the value to a String before it is displayed).
  • Title (Optional) : The title for the input box. If this argument is removed, he default title is “Input.” It is a variant parameter.
  • Default (Optional) : Specifies a value that will appear in the text box when the dialog box is initially displayed like water mark in advanced or modern controls.
  • Left (Optional) : Specifies left (x) position for the dialog box in the screen.
  • Top (Optional) : Specifies top (y) position for the dialog box in the screen.
  • HelpFile (Optional) : The name of the Help file for this input box. If the HelpFile and HelpContextID arguments are present, a Help button will appear in the dialog box. variant data type.
  • HelpContextID (Optional) : The context ID number of the Help topic in HelpFile. variant data type.
  • Type (Optional) : Specifies the return data type. If this argument is omitted, the dialog box returns text. variant data type. Followings are the Type which can be applied to customized InputBox:

Types

ValueDescription
0Formula
1Number
2Text (string)
4Logical value (true/false)
8Cell reference, Range object
16Error value like N#A
64Array of values

Code example

Public Sub InputBoxCodeExample()
    On Error GoTo errh
    Dim rng As Range
    Set rng = Application.InputBox(prompt:="Get Range", Type:=8)
    If rng Is Nothing Then
        MsgBox "You selected nothing"
        Exit Sub
    Else
        MsgBox "You Selected : " & rng.Address
        Exit Sub
    End If
    
errh:
    If Err.Number <> 0 Then
        Debug.Print Err.Description
    End If
End Sub

Next>>Manage Alert Dialog Windows in VBA

Leave a Reply

Your email address will not be published. Required fields are marked *