The InputBox function
The InputBox function is useful for obtaining a single input from the user. A simplified version of the function’s syntax follows:
InputBox function: Syntax
InputBox ( prompt, [title], [default] )
InputBox function: Parameters or Arguments
The arguments are defined as follows:
- prompt: This one is required. This text is displayed in the input box.
- title: It is optional. This text is displayed in the input box’s title bar.
- default: It is also optional. The default value showed in the input field.
InputBox Function: Example
The following macro (StandardInputBox) uses an InputBox function. Observe closely.
Sub StandardInputBox() Dim stringName As String 'InputBox(prompt[, title] [, default] ) stringName = InputBox("Enter you name:", "NAME ENTRY BOX", "Enter Your Name") 'Exit sub if user press Cancel button or does not enter any text in the Input field. If stringName = vbNullString Then Exit Sub MsgBox "Hello " & stringName End Sub
Run this VBA macro using Developer ⇒ Code ⇒ Macro ⇒ Select StandardInputBox Macro ⇒ Run. You will see a figure like below:
I write my name in the Input field as “Kawser Ahmed” and then click the OK button, it will show another dialog box as I have used this code in the macro:
MsgBox "Hello " & stringName
Now I click OK and the dialog box vanishes. This is how you can use InputBox function in a macro instead of writing a UserForm.
Happy Excelling ☕