InputBox Function in Excel VBA

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:

Alternatives of UserForm in Excel: InputBox & MsgBox Functions

Standard InputBox dialog box, it takes one value from the user.

I write my name in the Input field as “Kawser Ahmed” and then click OK button, it will show another dialog box as I have used this code in the macro:

MsgBox "Hello " & stringName 
Alternatives of UserForm in Excel: InputBox & MsgBox Functions

Showing my name in the message box.

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 ☕

Read More…

MsgBox Function in Excel VBA

Hello! Welcome to my Excel blog! It took me some time to be a fan of Excel. But now I am a die-hard fan of MS Excel. I learn new ways of doing things with Excel and share here. Not only how to guide on Excel, but you will get also topics on Finance, Statistics, Data Analysis, and BI. Stay tuned!