In this VBA Tutorial, you learn how to create input boxes with both the InputBox function and the Application.InputBox method. This includes:
- How to create an InputBox.
- How to create an InputBox with multiple lines.
- How to create an InputBox that works with a specific type of data.
- How to handle the cases where the user clicks the Cancel button of the InputBox.
This VBA InputBox Tutorial is accompanied by an Excel workbook containing the macros I use in the examples below. You can get immediate access to this example workbook by clicking the button below.
Table of Contents
Related VBA and Macro Tutorials
The following VBA and Macro Tutorials may help you better understand and implement the contents below:
- General VBA constructs and structures:
- Read a Macro Tutorial for beginners here.
- Learn the definitions of several basic and important VBA terms here.
- Learn how to specify macro security settings here.
- Learn how to work with the Visual Basic Editor here.
- Learn about the Excel VBA Object Model here.
- Learn how to refer to cell ranges here.
- Learn how to create Sub procedures here.
- Learn how to work with object properties here.
- Learn how to work with object methods here.
- Learn how to declare and work with variables here.
- Learn how to work with data types here.
- Learn how to work with functions here.
- Learn how to work with loops here.
- Learn how to work with arrays here.
- Practical VBA applications and macro examples:
You can find additional VBA and Macro Tutorials in the Archives.
#1: Create InputBox with InputBox function
VBA code to create InputBox with InputBox function
To create a basic InputBox with the VBA InputBox function, use a statement with the following structure:
InputBoxVariable = InputBox(Prompt:=PromptString, Title:=TitleString, Default:=DefaultInputString)
Process to create InputBox with InputBox function
To create a basic InputBox with the VBA InputBox function, follow these steps:
- Create an input box with the InputBox function (InputBox(…)).
- Assign the value returned by the InputBox function to a variable (InputBoxVariable = InputBox(…)).
VBA statement explanation
Item: InputBoxVariable
InputBoxVariable is the variable you want to hold the value returned by the InputBox function.
The InputBox function returns a String.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: InputBox(…)
The InputBox function:
- Displays an input box;
- Waits for the user to either (i) input text and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns a string with the contents of the text box in the input box (when the user clicks OK or presses Enter).
If you want to handle the cases where the user clicks on the Cancel button or presses Esc, please refer to the appropriate section of this Tutorial.
Item: Prompt:=PromptString
The Prompt argument of the InputBox function is a string displayed as the message in the input box. Prompt is a required argument.
You generally specify PromptString as a string expression.
The maximum length of PromptString is roughly 1024 characters. However, this maximum length may vary slightly depending on the width of the characters within PromptString.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Item: Title:=TitleString
The Title argument of the InputBox function is a string expression displayed as the title in the title bar of the input box.
Title is an optional argument. If you omit the Title argument, the title of the input box is “Microsoft Excel”.
You generally specify TitleString as a string expression.
Item: Default:=DefaultInputString
The Default argument of the InputBox function is a string expression displayed inside the text box of the input box. DefaultInputString is, therefore, the default response.
Default is an optional argument. If you omit the Default argument, the text box is empty.
Macro example to create InputBox with InputBox function
The following macro example:
- Creates a basic input box with the InputBox function.
- Assigns the value returned by the InputBox function to a variable (myInputBoxVariable = inputBox(…)).
- Displays a message box with the value held by the variable.
Sub CreateInputBoxFunction() 'source: https://powerspreadsheets.com/ 'creates an input box with the InputBox function 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxVariable As String '(1) create InputBox, and (2) assign value returned by InputBox function to variable myInputBoxVariable = inputBox(Prompt:="Create Excel VBA InputBox", Title:="This is an Excel VBA InputBox", Default:="Enter VBA InputBox value here") 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxVariable End Sub
Effects of executing macro example to create InputBox with InputBox function
The following GIF illustrates the results of executing the macro example. As expected, Excel displays a basic input box using the InputBox function.
#2: Create InputBox with Application.InputBox method
VBA code to create InputBox with Application.InputBox method
To create a basic InputBox with the VBA Application.InputBox method, use a statement with the following structure:
InputBoxVariable = Application.InputBox(Prompt:=PromptString, Title:=TitleString, Default:=DefaultInput)
Process to create InputBox with Application.InputBox method
To create a basic InputBox with the VBA Application.InputBox method, follow these steps:
- Create an input box with the Application.InputBox method (Application.InputBox(…)).
- Assign the value returned by the Application.InputBox method to a variable (InputBoxVariable = Application.InputBox(…)).
VBA statement explanation
Item: InputBoxVariable
InputBoxVariable is the variable you want to hold the value returned by the Application.InputBox method.
The Application.InputBox method returns a Variant.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: Application.InputBox(…)
The Application.InputBox method:
- Displays an input box'
- Waits for the user to either (i) input information and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns the information entered in the dialog box (if the user clicks OK or presses Enter) or the False Boolean value (if the user clicks Cancel).
Item: Prompt:=PromptString
The Prompt parameter of the Application.InputBox method is a string displayed as the message in the input box. Prompt is a required parameter.
You generally specify PromptString as a string expression.
You can also specify PromptString as a number, a date or a Boolean. In such cases, Excel coerces the number, date or Boolean to a string.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Item: Title:=TitleString
The Title parameter of the Application.InputBox method is the title displayed in the title bar of the input box.
Title is an optional parameter. If you omit the Title parameter, the title of the input box is “Input”.
The Title parameter is of the Variant data type.
Item: Default:=DefaultInput
The Default parameter of the Application.InputBox method is the value displayed inside the text box of the input box. DefaultInput is, therefore, the default response.
Default is an optional parameter. If you omit the Default parameter, the text box is empty.
The Default parameter is of the Variant data type.
Macro example to create InputBox with Application.InputBox method
The following macro example:
- Creates a basic input box with the Application.InputBox method.
- Assigns the value returned by the Application.InputBox method to a variable (myInputBoxVariable = Application.inputBox(…)).
- Displays a message box with the value held by the variable.
Sub CreateInputBoxMethod() 'source: https://powerspreadsheets.com/ 'creates an input box with the Application.InputBox method 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxVariable As Variant '(1) create InputBox, and (2) assign value returned by Application.InputBox method to variable myInputBoxVariable = Application.inputBox(Prompt:="Create Excel VBA InputBox", Title:="This is an Excel VBA InputBox", Default:="Enter VBA InputBox value here") 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxVariable End Sub
Effects of executing macro example to create InputBox with Application.InputBox method
The following GIF illustrates the results of executing the macro example. As expected, Excel displays a basic input box using the Application.InputBox method.
#3: Create InputBox with multiple lines using InputBox function
VBA code to create InputBox with multiple lines using InputBox function
To create an InputBox containing multiple lines with the VBA InputBox function, use a statement with the following structure:
InputBoxMultipleLinesVariable = inputBox(Prompt:=PromptString1 & NewLineCharacter & PromptString2 & ... & NewLineCharacter & PromptString#)
Process to create InputBox with multiple lines using InputBox function
To create an InputBox containing multiple lines with the VBA InputBox function, follow these steps:
- Create an input box with the InputBox function (InputBox(…)).
- Specify the message displayed in the message box (Prompt:=PromptString1 & NewLineCharacter & PromptString2 & … & NewLineCharacter & PromptString#) as an appropriately concatenated (with the & character) combination of:
- Strings (PromptString1, PromptString2, …, PromptString#); and
- Characters that create a new line or line break (NewLineCharacter).
- Assign the value returned by the InputBox function to a variable (InputBoxMultipleLinesVariable = InputBox(…)).
VBA statement explanation
Item: InputBoxMultipleLinesVariable
InputBoxMultipleLinesVariable is the variable you want to hold the value returned by the InputBox function.
The InputBox function returns a String.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: inputBox(…)
The InputBox function:
- Displays an input box;
- Waits for the user to either (i) input text and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns a string with the contents of the text box in the input box (when the user clicks OK or presses Enter).
If you want to handle the cases where the user clicks on the Cancel button or presses Esc, please refer to the appropriate section of this Tutorial.
Item: Prompt:=PromptString1 & NewLineCharacter & PromptString2 & … & NewLineCharacter & PromptString#
The Prompt argument of the InputBox function is a string displayed as the message in the input box. Prompt is a required argument.
You generally specify Prompt as a string expression.
The maximum length of Prompt is roughly 1024 characters. However, this maximum length may vary slightly depending on the width of the characters you work with.
To create an input box with multiple lines, you build the string expression assigned to Prompt by concatenating as many strings (PromptString1, PromptString2, …, PromptString#) and new line characters (NewLineCharacter) as required. For these purposes:
- PromptString1, PromptString2, …, PromptString# are the strings (excluding the new line characters) that determine the message in the input box.
- The & operator carries out string concatenation. Therefore, & concatenates the different strings and new line characters.
- NewLineCharacter is a character or character combination returning 1 of the following:
- Carriage return.
- Linefeed.
- Carriage return linefeed combination.
- New line (which is platform specific).
Specify NewLineCharacter using any of the constants or character codes (with the Chr function) listed below:
Constant | Equivalent Chr function | General description |
vbLf | Chr(10) | Linefeed |
vbCr | Chr(13) | Carriage return |
vbCrLf | Chr(13) & Chr(10) | Carriage return linefeed combination |
vbNewLine | Chr(13) & Chr(10) in Excel for Windows or Chr(13) in Excel for Mac | New line character, which is platform specific |
Macro example to create InputBox with multiple lines using InputBox function
The following macro example:
- Creates an input box containing multiple lines (Create Excel VBA InputBox” & vbNewLine & “with multiple lines) with the InputBox function.
- Assigns the value returned by the InputBox function to a variable (myInputBoxMultipleLinesVariable = inputBox(…)).
- Displays a message box with the value held by the variable.
Sub CreateInputBoxFunctionMultipleLines() 'source: https://powerspreadsheets.com/ 'creates an input box with multiple lines using the InputBox function 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxMultipleLinesVariable As String '(1) create InputBox with multiple lines, and (2) assign value returned by InputBox function to variable myInputBoxMultipleLinesVariable = inputBox(Prompt:="Create Excel VBA InputBox" & vbNewLine & "with multiple lines") 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxMultipleLinesVariable End Sub
Effects of executing macro example to create InputBox with multiple lines using InputBox function
The following GIF illustrates the results of executing the macro example. As expected, Excel displays an input box containing multiple lines using the InputBox function.
#4: Create InputBox with multiple lines using Application.InputBox method
VBA code to create InputBox with multiple lines using Application.InputBox method
To create an InputBox containing multiple lines with the VBA Application.InputBox method, use a statement with the following structure:
InputBoxMultipleLinesVariable = Application.InputBox(Prompt:=PromptString1 & NewLineCharacter & PromptString2 & ... & NewLineCharacter & PromptString#)
Process to create InputBox with multiple lines using Application.InputBox method
To create an InputBox containing multiple lines with the VBA Application.InputBox method, follow these steps:
- Create an input box with the Application.InputBox method (Application.InputBox(…)).
- Specify the message displayed in the message box (Prompt:=PromptString1 & NewLineCharacter & PromptString2 & … & NewLineCharacter & PromptString#) as an appropriately concatenated (with the & character) combination of:
- Strings (PromptString1, PromptString2, …, PromptString#); and
- Characters that create a new line or line break (NewLineCharacter).
- Assign the value returned by the Application.InputBox method to a variable (InputBoxMultipleLinesVariable = Application.InputBox(…)).
VBA statement explanation
Item: InputBoxMultipleLinesVariable
InputBoxMultipleLinesVariable is the variable you want to hold the value returned by the Application.InputBox method.
The Application.InputBox method returns a Variant.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: Application.InputBox(…)
The Application.InputBox method:
- Displays an input box'
- Waits for the user to either (i) input information and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns the information entered in the dialog box (if the user clicks OK or presses Enter) or the False Boolean value (if the user clicks Cancel).
Item: Prompt:=PromptString1 & NewLineCharacter & PromptString2 & … & NewLineCharacter & PromptString#
The Prompt parameter of the Application.InputBox method is a string displayed as the message in the input box. Prompt is a required parameter.
You generally specify Prompt as a string expression. You can also specify Prompt as a number, a date or a Boolean. In such cases, Excel coerces the number, date or Boolean to a string.
To create an input box with multiple lines, you build the expression assigned to Prompt by concatenating as many strings (PromptString1, PromptString2, …, PromptString#) and new line characters (NewLineCharacter) as required. For these purposes:
- PromptString1, PromptString2, …, PromptString# are the strings (excluding the new line characters) that determine the message in the input box.
- The & operator carries out string concatenation. Therefore, & concatenates the different strings and new line characters.
- NewLineCharacter is a character or character combination returning 1 of the following:
- Carriage return.
- Linefeed.
- Carriage return linefeed combination.
- New line (which is platform specific).
Specify NewLineCharacter using any of the constants or character codes (with the Chr function) listed below:
Constant | Equivalent Chr function | General description |
vbLf | Chr(10) | Linefeed |
vbCr | Chr(13) | Carriage return |
vbCrLf | Chr(13) & Chr(10) | Carriage return linefeed combination |
vbNewLine | Chr(13) & Chr(10) in Excel for Windows or Chr(13) in Excel for Mac | New line character, which is platform specific |
Macro example to create InputBox with multiple lines using Application.InputBox method
The following macro example:
- Creates an input box containing multiple lines (Create Excel VBA InputBox” & vbNewLine & “with multiple lines) with the Application.InputBox method.
- Assigns the value returned by the Application.InputBox method to a variable (myInputBoxMultipleLinesVariable = Application.inputBox(…)).
- Displays a message box with the value held by the variable.
Sub CreateInputBoxMethodMultipleLines() 'source: https://powerspreadsheets.com/ 'creates an input box with multiple lines using the Application.InputBox method 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxMultipleLinesVariable As Variant '(1) create InputBox with multiple lines, and (2) assign value returned by Application.InputBox method to variable myInputBoxMultipleLinesVariable = Application.inputBox(Prompt:="Create Excel VBA InputBox" & vbNewLine & "with multiple lines") 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxMultipleLinesVariable End Sub
Effects of executing macro example to create InputBox with multiple lines using Application.InputBox method
The following GIF illustrates the results of executing the macro example. As expected, Excel displays an input box containing multiple lines using the Application.InputBox method.
#5: Create InputBox that works with a specific data type using InputBox function
VBA code to create InputBox that works with a specific data type using InputBox function
To create an InputBox that works with a specific data type with the VBA InputBox function, use a macro with the following statement structure:
InputBoxTypeVariable = InputBox(Prompt:=PromptString) If IsFunction(InputBoxTypeVariable) Then StatementsIfInputIsType Else StatementsIfInputIsNotType End If
Process to create InputBox that works with a specific data type using InputBox function
To create an InputBox that works with a specific data type with the VBA InputBox function, follow these steps:
- Create an input box with the InputBox function (InputBox(…)).
- Assign the value returned by the InputBox function to a variable (InputBoxTypeVariable = InputBox(…)).
- Use an If… Then… Else statement for the following:
- Testing whether the type of data held by the variable is the one you want to work with (IsFunction(InputBoxTypeVariable)).
- Executing the appropriate group of statements depending on whether the type of data held by the variable is the one you want to work with (StatementsIfInputIsType) or not (StatementsIfInputIsNotType).
VBA statement explanation
Line #1: InputBoxTypeVariable = InputBox(Prompt:=PromptString)
Item: InputBoxTypeVariable
InputBoxTypeVariable is the variable you want to hold the value returned by the InputBox function.
The InputBox function returns a String.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: InputBox(…)
The InputBox function:
- Displays an input box;
- Waits for the user to either (i) input text and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns a string with the contents of the text box in the input box (when the user clicks OK or presses Enter).
If you want to handle the cases where the user clicks on the Cancel button or presses Esc, please refer to the appropriate section of this Tutorial.
Item: Prompt:=PromptString
The Prompt argument of the InputBox function is a string displayed as the message in the input box. Prompt is a required argument.
You generally specify PromptString as a string expression.
The maximum length of PromptString is roughly 1024 characters. However, this maximum length may vary slightly depending on the width of the characters within PromptString.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Lines #2, #4 and #6: If IsFunction(InputBoxTypeVariable) Then | Else | End If
Item: If… Then… Else… End If
The If… Then… Else statement conditionally executes a group of statements (StatementsIfInputIsType or StatementsIfInputIsNotType) depending on the value of an expression (Isfunction(InputBoxTypeVariable)).
Item: IsFunction(InputBoxTypeVariable)
The condition of the If… Then… Else statement is an expression returning True or False.
When you work with an input box and a specific data type using this macro structure, you can check the type of data held by InputBoxTypeVariable by working with certain VBA built-in functions (an IsFunction), as appropriate. These include the following functions:
Function | Returns True if InputBoxTypeVariable… | Returns False if InputBoxTypeVariable… |
IsDate | Is a date or recognizable as a valid date | Isn't date or isn't recognizable as a valid date |
IsError | Is an error value | Isn't an error value |
IsNumeric | Can be evaluated/recognized as a number | Can't be evaluated/recognized as a number |
Line #3: StatementsIfInputIsType
Statements executed if the tested condition (IsFunction(InputBoxTypeVariable)) returns True. In other words, these statements are executed if the input entered by the user in the input box is of the appropriate type.
Line #5: StatementsIfInputIsNotType
Statements executed if the tested condition (IsFunction(InputBoxTypeVariable)) returns False. In other words, these statements are executed if the input entered by the user in the input box isn't of the appropriate type.
Macro example to create InputBox that works with a specific data type using InputBox function
The following macro example:
- Creates an input box with the InputBox function.
- Assigns the value returned by the InputBox function to a variable (myInputBoxTypeVariable = inputBox(…)).
- Checks whether the value held by the variable is numeric (IsNumeric(myInputBoxTypeVariable)).
- If the value is numeric, displays a message box with the value held by the variable.
- If the value isn't numeric, displays a message box asking the user to try again and enter a number.
Sub CreateInputBoxFunctionDataType() 'source: https://powerspreadsheets.com/ 'creates an input box that works with a number using the InputBox function 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxTypeVariable As String '(1) create InputBox that works with a number, and (2) assign value returned by InputBox function to variable myInputBoxTypeVariable = inputBox(Prompt:="Create Excel VBA InputBox that works with a number") 'check if user entered a number and, if appropriate, execute statements If IsNumeric(myInputBoxTypeVariable) Then 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxTypeVariable 'if user didn't enter a number, execute statements Else 'display message box confirming that user didn't enter a number MsgBox "Please try again and enter a number" End If End Sub
Effects of executing macro example to create InputBox that works with a specific data type using InputBox function
The following GIF illustrates the results of executing the macro example. As expected:
- Excel identifies whether the input box created with the InputBox function contains a number; and
- Displays the appropriate message box.
#6: Create InputBox that works with a specific data type using Application.InputBox method
VBA code to create InputBox that works with a specific data type using Application.InputBox method
To create an InputBox that works with a specific data type with the VBA Application.InputBox method, use a statement with the following structure:
InputBoxTypeVariable = Application.InputBox(Prompt:=PromptString, Type:=TypeValue)
Process to create InputBox that works with a specific data type using Application.InputBox method
To create an InputBox that works with a specific data type with the VBA Application.InputBox method, follow these steps:
- Create an input box with the Application.InputBox method (Application.InputBox(…)).
- Specify the data type you want to work with by working with the Type parameter of the Application.InputBox method (Type:=TypeValue).
- Assign the value returned by the Application.InputBox method to a variable (InputBoxTypeVariable = Application.InputBox(…)).
VBA statement explanation
Item: InputBoxTypeVariable
InputBoxTypeVariable is the variable you want to hold the value returned by the Application.InputBox method.
The Application.InputBox method returns a Variant.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: Application.InputBox(…)
The Application.InputBox method:
- Displays an input box'
- Waits for the user to either (i) input information and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns the information entered in the dialog box (if the user clicks OK or presses Enter) or the False Boolean value (if the user clicks Cancel).
Item: Prompt:=PromptString
The Prompt parameter of the Application.InputBox method is a string displayed as the message in the input box. Prompt is a required parameter.
You generally specify PromptString as a string expression.
You can also specify PromptString as a number, a date or a Boolean. In such cases, Excel coerces the number, date or Boolean to a string.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Item: Type:=TypeValue
The Type parameter of the Application.InputBox method specifies the data type returned.
Set the Type parameter using the values listed below. If required, you can set the Type parameter to be a sum of several of these values.
Value | Basic description | Additional comments |
0 | Formula | Application.InputBox returns the formula in the form of text. Cell references inside the formula are returned as A1-style references. |
1 | Number | |
2 | Text | |
4 | Boolean | |
8 | Range object | Use the Set statement to assign the Range object returned by Application.InputBox to an object variable. |
16 | Error | |
64 | Array of values |
Type is an optional parameter. If you omit the Type parameter, the Application.InputBox method returns text.
Macro example to create InputBox that works with a specific data type using Application.InputBox method
The following macro example:
- Creates an input box that returns a number (Type:=1) with the Application.InputBox method.
- Assigns the value returned by the Application.InputBox method to a variable (myInputBoxTypeVariable = Application.inputBox(…)).
- Displays a message box with the value held by the variable.
Sub CreateInputBoxMethodDataType() 'source: https://powerspreadsheets.com/ 'creates an input box that works with a number using the Application.InputBox method 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxTypeVariable As Variant '(1) create InputBox that works with a number, and (2) assign value returned by Application.InputBox method to variable myInputBoxTypeVariable = Application.inputBox(Prompt:="Create Excel VBA InputBox that works with a number", Type:=1) 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxTypeVariable End Sub
Effects of executing macro example to create InputBox that works with a specific data type using Application.InputBox method
The following GIF illustrates the results of executing the macro example. As expected:
- Excel identifies whether the input box created with the Application.InputBox method contains a number; and
- If the entered input isn't a number, displays a warning.
#7: Create InputBox and check if user clicks Cancel button with InputBox function
VBA code to create InputBox and check if user clicks Cancel button with InputBox function
To create an InputBox with the VBA InputBox function and check if the user clicks Cancel, use a macro with the following statement structure:
InputBoxCancelVariable = InputBox(Prompt:=PromptString) If StrPtr(InputBoxCancelVariable) = 0 Then StatementsIfCancel ElseIf InputBoxCancelVariable = "" Then StatementsIfNoInput Else StatementsIfInputAndOK End If
Process to create InputBox and check if user clicks Cancel button with InputBox function
To create an InputBox that works with a specific data type with the VBA InputBox function, follow these steps:
- Create an input box with the InputBox function (InputBox(…)).
- Assign the value returned by the InputBox function to a variable (InputBoxCancelVariable = InputBox(…)).
- Use an If… Then… Else statement for the following:
- Testing whether the user clicked Cancel (StrPtr(InputBoxCancelVariable) = 0) or entered no input prior to clicking OK (InputBoxCancelVariable = “”).
- Executing the appropriate group of statements depending on whether the user clicked Cancel (StatementsIfCancel), entered no input prior to clicking OK (StatementsIfNoInput) or entered input and clicked OK (StatementsIfInputAndOK).
VBA statement explanation
Line #1: InputBoxCancelVariable = InputBox(Prompt:=PromptString)
Item: InputBoxCancelVariable
InputBoxCancelVariable is the variable you want to hold the value returned by the InputBox function.
The InputBox function returns a String.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: InputBox(…)
The InputBox function:
- Displays an input box;
- Waits for the user to either (i) input text and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns a string with the contents of the text box in the input box (when the user clicks OK or presses Enter).
According to the Microsoft Developer Network, the InputBox function returns a zero-length string (“”) when the user clicks Cancel (or presses Esc). When checking if the user clicks Cancel using this macro structure, you rely on a quirk of the InputBox function which allows you to work with StrPtr.
Item: Prompt:=PromptString
The Prompt argument of the InputBox function is a string displayed as the message in the input box. Prompt is a required argument.
You generally specify PromptString as a string expression.
The maximum length of PromptString is roughly 1024 characters. However, this maximum length may vary slightly depending on the width of the characters within PromptString.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Lines #2, #6 and #8: If StrPtr(InputBoxCancelVariable) = 0 Then | Else | End If
Item: If… Then… Else… End If
The If… Then… Else statement conditionally executes a group of statements (StatementsIfCancel, StatementsIfNoInput or StatementsIfInputAndOK) depending on the value of an expression (StrPtr(InputBoxCancelVariable) = 0 or InputBoxCancelVariable = “”).
Item: StrPtr(InputBoxCancelVariable) = 0
The condition of the If… Then… Else statement is an expression returning True or False.
When you check if the user clicks the Cancel button using this macro structure, you can work with the StrPtr function. StrPtr is an undocumented function. You can usually work with the StrPtr function to obtain the address of a variable.
When the user clicks Cancel, no string is assigned to InputBoxCancelVariable. Therefore, if the user clicks Cancel, StrPtr(InputBoxCancelVariable) = 0 returns True.
Line #3: StatementsIfCancel
Statements executed if the tested condition (StrPtr(InputBoxCancelVariable) = 0) returns True. In other words, these statements are executed if the user clicks Cancel.
Line #4: ElseIf InputBoxCancelVariable = “” Then
Item: ElseIf… Then
The If… Then… Else statement conditionally executes a group of statements (StatementsIfCancel, StatementsIfNoInput or StatementsIfInputAndOK) depending on the value of an expression (StrPtr(InputBoxCancelVariable) = 0 or InputBoxCancelVariable = “”).
Item: InputBoxCancelVariable = “”
The condition-n of the If… Then… Else statement is an expression returning True or False.
You can check if the user didn't enter any input prior to clicking OK by testing whether InputBoxCancelVariable holds a zero-length string (“”). In other words, if the user doesn't enter any input and clicks the OK button, InputBoxCancelVariable = 0 returns True.
Line #5: StatementsIfNoInput
Statements executed if the tested condition (InputBoxCancelVariable = “”) returns True. In other words, these statements are executed if the user doesn't enter any input and clicks the OK button.
Line #7: StatementsIfInputAndOK
Statements executed if none of the tested conditions (StrPtr(InputBoxCancelVariable) = 0 or InputBoxCancelVariable = “”) return True. In other words, these statements are executed if the user enters an input and clicks the OK button.
Macro example to create InputBox and check if user clicks Cancel button with InputBox function
The following macro example:
- Creates an input box with the InputBox function.
- Assigns the value returned by the InputBox function to a variable (myInputBoxCancelVariable = inputBox(…)).
- Checks whether user clicked Cancel (StrPtr(myInputBoxCancelVariable) = 0). If the user clicked Cancel, displays a message box confirming this.
- If the user didn't click Cancel, checks whether the user entered no input prior to clicking OK (myInputBoxCancelVariable = “”). If the user entered no input prior to clicking OK, displays a message box confirming this.
- If the user entered input and clicked OK, displays a message box with the value held by the variable.
Sub CreateInputBoxFunctionCancel() 'source: https://powerspreadsheets.com/ '(1) creates an input box with the InputBox function, and (2) handles case where user clicks Cancel button 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxCancelVariable As String '(1) create InputBox, and (2) assign value returned by InputBox function to variable myInputBoxCancelVariable = inputBox(Prompt:="Create Excel VBA InputBox and work with Cancel button") 'check if user clicked Cancel button and, if appropriate, execute statements If StrPtr(myInputBoxCancelVariable) = 0 Then 'display message box confirming that user clicked Cancel button MsgBox "You clicked the Cancel button" 'check if user entered no input and, if appropriate, execute statements ElseIf myInputBoxCancelVariable = "" Then 'display message box confirming that user entered no input MsgBox "You didn't enter an input" 'if user didn't click Cancel button and entered input, execute statements Else 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxCancelVariable End If End Sub
Effects of executing macro example to create InputBox and check if user clicks Cancel button with InputBox function
The following GIF illustrates the results of executing the macro example. As expected:
- Excel displays an input box created with the InputBox function.
- The macro identifies whether the user:
- Clicks the Cancel button;
- Enters no input prior to clicking OK; or
- Enters input and clicks OK.
- Excel displays the appropriate message box depending on the actions taken by the user.
#8: Create InputBox and check if user clicks Cancel button with Application.InputBox method
VBA code to create InputBox and check if user clicks Cancel button with Application.InputBox method
To create an InputBox with the VBA Application.InputBox method and check if the user clicks Cancel, use a macro with the following statement structure:
InputBoxCancelVariable = Application.InputBox(Prompt:=PromptString) If (TypeName(InputBoxCancelVariable) = "Boolean") And (InputBoxCancelVariable = "False") Then StatementsIfCancel ElseIf InputBoxCancelVariable = "" Then StatementsIfNoInput Else StatementsIfInputAndOK End If
Process to create InputBox and check if user clicks Cancel button with Application.InputBox method
To create an InputBox with the VBA Application.InputBox method and check if the user clicks Cancel, follow these steps:
- Create an input box with the Application.InputBox method (Application.InputBox(…)).
- Assign the value returned by the Application.InputBox method to a variable (InputBoxCancelVariable = Application.InputBox(…)).
- Use an If… Then… Else statement for the following:
- Testing whether the user clicked Cancel ((TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”)) or entered no input prior to clicking OK (InputBoxCancelVariable = “”).
- Executing the appropriate group of statements depending on whether the user clicked Cancel (StatementsIfCancel), entered no input prior to clicking OK (StatementsIfNoInput) or entered input and clicked OK (StatementsIfInputAndOK).
VBA statement explanation
Line #1: InputBoxCancelVariable = Application.InputBox(Prompt:=PromptString)
Item: InputBoxCancelVariable
InputBoxCancelVariable is the variable you want to hold the value returned by the Application.InputBox method.
The Application.InputBox method returns a Variant.
Item: =
The = operator assigns a value to a variable or property.
Use the = operator to assign the value returned by the InputBox function (InputBox(…)) to InputBoxVariable.
Item: Application.InputBox(…)
The Application.InputBox method:
- Displays an input box'
- Waits for the user to either (i) input information and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns the information entered in the dialog box (if the user clicks OK or presses Enter) or the False Boolean value (if the user clicks Cancel).
Item: Prompt:=PromptString
The Prompt parameter of the Application.InputBox method is a string displayed as the message in the input box. Prompt is a required parameter.
You generally specify PromptString as a string expression.
You can also specify PromptString as a number, a date or a Boolean. In such cases, Excel coerces the number, date or Boolean to a string.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Lines #2, #6 and #8: If (TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”) Then | Else | End If
Item: If… Then… Else… End If
The If… Then… Else statement conditionally executes a group of statements (StatementsIfCancel, StatementsIfNoInput or StatementsIfInputAndOK) depending on the value of an expression (((TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”)) or InputBoxCancelVariable = “”).
Item: TypeName(InputBoxCancelVariable) = “Boolean” And InputBoxCancelVariable = “False”
The condition of the If… Then… Else statement is an expression returning True or False.
The Application.InputBox method returns the False Boolean value when the user clicks Cancel. Therefore, when you check if the user clicks the Cancel button using this macro structure, you test whether 2 conditions are met.
Condition #1: TypeName(InputBoxCancelVariable) = “Boolean”
TypeName(InputBoxCancelVariable) = “Boolean” checks whether InputBoxCancelVariable is a Boolean value.
For these purposes, work with the TypeName function, which returns a string with information about the variable passed as argument (InputBoxCancelVariable). Therefore, TypeName(InputBoxCancelVariable) = “Boolean”:
- Returns True if InputBoxCancelVariable is a Boolean. This occurs, among others, when the user clicks Cancel.
- Returns False if InputBoxCancelVariable isn't a Boolean.
Condition #2: InputBoxCancelVariable = “False”
InputBoxCancelVariable = “False” checks whether InputBoxCancelVariable holds the string “False”. Therefore, InputBoxCancelVariable = “False”:
- Returns True if InputBoxCancelVariable holds “False”. This occurs, among others, when the user clicks Cancel.
- Returns False if InputBoxCancelVariable doesn't hold “False”.
Condition #1 And Condition #2
When you check if the user clicks the Cancel button using this macro structure, both conditions #1 (TypeName(InputBoxCancelVariable) = “Boolean”) and #2 (InputBoxCancelVariable = “False”) must be met.
The And operator performs a logical conjunction. Therefore, the condition of the If… Then… else statement returns True if the user clicks Cancel.
Line #3: StatementsIfCancel
Statements executed if the tested condition ((TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”)) returns True. In other words, these statements are executed if the user clicks Cancel.
Line #4: ElseIf InputBoxCancelVariable = “” Then
Item: ElseIf… Then
The If… Then… Else statement conditionally executes a group of statements (StatementsIfCancel, StatementsIfNoInput or StatementsIfInputAndOK) depending on the value of an expression (((TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”)) or InputBoxCancelVariable = “”).
Item: InputBoxCancelVariable = “”
The condition-n of the If… Then… Else statement is an expression returning True or False.
You can check if the user didn't enter any input prior to clicking OK by testing whether InputBoxCancelVariable holds a zero-length string (“”). In other words, if the user doesn't enter any input and clicks the OK button, InputBoxCancelVariable = 0 returns True.
Line #5: StatementsIfNoInput
Statements executed if the tested condition (InputBoxCancelVariable = “”) returns True. In other words, these statements are executed if the user doesn't enter any input and clicks the OK button.
Line #7: StatementsIfInputAndOK
Statements executed if none of the tested conditions (((TypeName(InputBoxCancelVariable) = “Boolean”) And (InputBoxCancelVariable = “False”)) or InputBoxCancelVariable = “”) return True. In other words, these statements are executed if the user enters an input and clicks the OK button.
Macro example to create InputBox and check if user clicks Cancel button with Application.InputBox method
The following macro example:
- Creates an input box with the Application.InputBox method.
- Assigns the value returned by the Application.InputBox method to a variable (myInputBoxCancelVariable = Application.inputBox(…)).
- Checks whether user clicked Cancel ((TypeName(myInputBoxCancelVariable) = “Boolean”) And (myInputBoxCancelVariable = “False”)). If the user clicked Cancel, displays a message box confirming this.
- If the user didn't click Cancel, checks whether the user entered no input prior to clicking OK (myInputBoxCancelVariable = “”). If the user entered no input prior to clicking OK, displays a message box confirming this.
- If the user entered input and clicked OK, displays a message box with the value held by the variable.
Sub CreateInputBoxMethodCancel() 'source: https://powerspreadsheets.com/ '(1) creates an input box with the Application.InputBox method, and (2) handles case where user clicks Cancel button 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare variable to hold value returned by InputBox Dim myInputBoxCancelVariable As Variant '(1) create InputBox, and (2) assign value returned by Application.InputBox method to variable myInputBoxCancelVariable = Application.inputBox(Prompt:="Create Excel VBA InputBox and work with Cancel button") 'check if user clicked Cancel button and, if appropriate, execute statements If (TypeName(myInputBoxCancelVariable) = "Boolean") And (myInputBoxCancelVariable = "False") Then 'display message box confirming that user clicked Cancel button MsgBox "You clicked the Cancel button" 'check if user entered no input and, if appropriate, execute statements ElseIf myInputBoxCancelVariable = "" Then 'display message box confirming that user entered no input MsgBox "You didn't enter an input" 'if user didn't click Cancel button and entered input, execute statements Else 'display message box with value held by variable MsgBox "Your input was: " & myInputBoxCancelVariable End If End Sub
Effects of executing macro example to create InputBox and check if user clicks Cancel button with Application.InputBox method
The following GIF illustrates the results of executing the macro example. As expected:
- Excel displays an input box created with the Application.InputBox function.
- The macro identifies whether the user:
- Clicks the Cancel button;
- Enters no input prior to clicking OK; or
- Enters input and clicks OK.
- Excel displays the appropriate message box depending on the actions taken by the user.
#9: Create InputBox and check if user clicks Cancel button when working with cell range and Application.InputBox method
VBA code to create InputBox and check if user clicks Cancel button when working with cell range and Application.InputBox method
To create an InputBox that works with a cell range using the VBA Application.InputBox method and check if the user clicks Cancel, use a macro with the following statement structure:
Dim InputBoxRangeCancelVariable As Range On Error Resume Next Set InputBoxRangeCancelVariable = Application.InputBox(Prompt:=PromptString, Type:=8) On Error GoTo 0 If InputBoxRangeCancelVariable Is Nothing Then StatementsIfCancel Else StatementsIfRangeInput End If
Process to create InputBox and check if user clicks Cancel button when working with cell range and Application.InputBox method
To create an InputBox that works with a cell range using the VBA Application.InputBox method and check if the user clicks Cancel, follow these steps:
- Explicitly declare an object variable to hold a reference to the Range object representing the cell range (Dim InputBoxRangeCancelVariable As Range).
- Enable error-handling with the On Error Resume Next statement.
- Create an input box with the Application.InputBox method (Application.InputBox(…)).
- Set the Type parameter of the Application.InputBox method to 8 (Type:=8), which results in Application.InputBox returning a Range object.
- Assign the value returned by the Application.InputBox method to the object variable (InputBoxRangeCancelVariable = Application.InputBox(…)).
- Disable error-handling withe the On Error GoTo 0 statement.
- Use an If… Then… Else statement for the following:
- Testing whether the user clicked Cancel (InputBoxRangeCancelVariable Is Nothing).
- Executing the appropriate group of statements depending on whether the user clicked Cancel (StatementsIfCancel) or not (StatementsIfRangeInput).
VBA statement explanation
Line #1: Dim InputBoxRangeCancelVariable As Range
The Dim statement declares the InputBoxRangeCancelVariable object variable as of the Range object data type and allocates storage space.
When you check if the user clicks the Cancel button while working with a cell range using this macro structure, you explicitly declare the object variable that holds the reference to the cell range returned by the Application.InputBox method.
Line #2: On Error Resume Next
The On Error Resume Next statement enables an error-handling routine and specifies that, when a run-time error occurs, control goes to the statement following that where the error occurred.
When you check if the user clicks the Cancel button while working with a cell range using this macro structure, On Error Resume Next handles the error caused by line #3 (Set InputBoxRangeCancelVariable = Application.InputBox(Prompt:=PromptString, Type:=8)) if the user clicks Cancel. This error is usually run-time error 424 (object required).
If you don't declare the InputBoxRangeCancelVariable object variable explicitly, the behavior of the macro and the error caused when the user clicks Cancel usually differs from what I describe in this VBA Tutorial.
Line #3: Set InputBoxRangeCancelVariable = Application.InputBox(Prompt:=PromptString, Type:=8)
Item: Set… =…
The Set statement assigns the object reference returned by the Application.InputBox method (Application.InputBox(…)) to InputBoxRangeCancelVariable.
Item: InputBoxRangeCancelVariable
InputBoxRangeCancelVariable is the object variable you want to hold the Range object returned by the Application.InputBox method.
When working with a cell range and the Application.InputBox method, Application.InputBox usually returns a Range object, unless the user clicks on the Cancel button. The cases where the user clicks on the Cancel button are handled by the On Error Resume Next statement.
Therefore, if you explicitly declare InputBoxRangeCancelVariable when working with this macro structure, you can usually declare it as of the Range object data type.
Item: Application.InputBox(…)
The Application.InputBox method:
- Displays an input box;
- Waits for the user to either (i) input information and click the OK button (or press the Enter key), or (ii) click the Cancel button (or press the Esc key); and
- Returns the information entered in the dialog box (if the user clicks OK or presses Enter).
Item: Prompt:=PromptString
The Prompt parameter of the Application.InputBox method is a string displayed as the message in the input box. Prompt is a required parameter.
You generally specify PromptString as a string expression.
You can also specify PromptString as a number, a date or a Boolean. In such cases, Excel coerces the number, date or Boolean to a string.
PromptString can be composed of multiple lines. To create an input box with multiple lines, please refer to the appropriate section of this Tutorial.
Item: Type:=8
The Type parameter of the Application.InputBox method specifies the data type returned.
When working with a cell range, set Type to 8. In such case, Application.InputBox returns a Range object.
Line #4: On Error GoTo 0
The On Error GoTo 0 statement disables the error-handler enabled in line #2.
Lines #5, #7 and #9: If InputBoxRangeCancelVariable Is Nothing Then | Else | End If
Item: If… Then… Else… End If
The If… Then… Else statement conditionally executes a group of statements (StatementsIfCancel or StatementsIfRangeInput) depending on the value of an expression (InputBoxRangeCancelVariable Is Nothing).
Item: InputBoxRangeCancelVariable Is Nothing
The condition of the If… Then… Else statement is an expression returning True or False.
The Is operator compares InputBoxRangeCancelVariable and Nothing. This expression returns True if both refer to the same.
Nothing is the default value for an object variable. Therefore, if the user clicks Cancel, InputBoxRangeCancelVariable Is Nothing returns True.
Line #6: StatementsIfCancel
Statements executed if the tested condition (InputBoxRangeCancelVariable Is Nothing) returns True. In other words, these statements are executed if the user clicks Cancel.
Line #8: StatementsIfRangeInput
Statements executed if the tested condition (InputBoxRangeCancelVariable Is Nothing) returns False. In other words, these statements are executed if the user enters/selects a cell range as input.
Macro example to create InputBox and check if user clicks Cancel button when working with cell range and Application.InputBox method
The following macro example:
- Enables error-handling (On Error Resume Next).
- Creates an input box that returns a Range object (Type:=8) with the Application.InputBox method.
- Assigns the object reference returned by the Application.InputBox method to an object variable (Set myInputBoxRangeCancelVariable = Application.inputBox(…)).
- Disables error-handling (On Error GoTo 0).
- Checks whether user clicked Cancel (myInputBoxRangeCancelVariable Is Nothing).
- If the user clicked Cancel, displays a message box confirming this.
- If the user didn't click Cancel, displays a message box with the range reference of the cell range represented by the variable (myInputBoxRangeCancelVariable.Address).
Sub CreateInputBoxMethodCellRangeCancel() 'source: https://powerspreadsheets.com/ '(1) creates an input box that works with cell ranges using the Application.InputBox method, and (2) handles case where user clicks Cancel button 'for further information: https://powerspreadsheets.com/excel-vba-inputbox/ 'declare object variable to hold reference to Range object (cell range) returned by InputBox Dim myInputBoxRangeCancelVariable As Range 'enable error-handling On Error Resume Next '(1) create InputBox that works with cell range, and (2) assign value returned by Application.InputBox method to variable Set myInputBoxRangeCancelVariable = Application.inputBox(Prompt:="Create Excel VBA InputBox that works with cell range and handles Cancel button", Type:=8) 'disable error-handling On Error GoTo 0 'check if user clicked Cancel button and, if appropriate, execute statements If myInputBoxRangeCancelVariable Is Nothing Then 'display message box confirming that user clicked Cancel button MsgBox "You clicked the Cancel button" 'if user didn't click Cancel button, execute statements Else 'display message box with address of cell range represented by object variable MsgBox "Your input was: " & myInputBoxRangeCancelVariable.Address End If End Sub
Effects of executing macro example to create InputBox and check if user clicks Cancel button when working with cell range and Application.InputBox method
The following GIF illustrates the results of executing the macro example. As expected:
- Excel displays an input box created with the Application.InputBox function. The InputBox allows the user to select a cell range.
- The macro identifies whether the user:
- Clicks the Cancel button; or
- Selects or otherwise enters an appropriate cell range.
- Excel displays the appropriate message box depending on the actions taken by the user.