Excel VBA Custom Input Box: 7 Examples

Example 1 – Customizing an Input Box with a Custom Prompt Message

Steps:

  • After opening a new module, insert the following code and run it by hitting the Run icon-
Sub customize_prompt_message_inputbox()
lookup_text = InputBox("Enter the employee name:")
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 4, False)
MsgBox "The employee salary is: " & "$" & result
End Sub

VBA code for customizing prompt message in custom input box

  • An Input Box will pop up according to the provided string. We will put a name as input and press OK.

vba custom input box

  • You’ll get a customized message box.

Final result with lookup value for the chosen name


Example 2 – Editing the InputBox Title for a Custom Input Box

You can edit the InputBox title as well.

Overview of editing Input Box title

Steps:

  • Insert a module and use the following code inside:
Sub customize_prompt_message_inputbox()
lookup_text = InputBox("Enter the employee name:", _
"Name Input")
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 4, False)
MsgBox "The employee salary is: " & "$" & result
End Sub

VBA code for customizing title to create custom input box

  • Click the Run icon or hit the F5 key from the keyboard to run.
  • You have the title for the InputBox named Name Input.
  • Put a name and click OK.

Insertion of name with customized title

  • You have the employee salary for the given name.

Final output with employee salary for the inserted name


Example 3 – Customizing VBA Input Box with Default Value

Here’s an example of a default input value.

Overview of customizing VBA Input Box with default value

Steps:

  • Insert a new module, place the below code, and Run.
Sub InputBox_Default_Value()
lookup_text = InputBox("Enter the employee name:", _
"Name Input", "Insert Name")
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 3, False)
MsgBox "The employee department is: " & result
End Sub

VBA code to customize default value to create custom input box

  • You have a default value inside the InputBox.

Input Box with default value

  • Put a name inside the box and select OK.

Insertion of name to lookup from the dataset

  • The final output will be in our hands.

Final result with lookup output for the inserted name


Example 4 – VBA Input Box with Custom Positions

The InputBox appears in the center of the screen, but you can customize the position of the dialog box by specifying the X and Y coordinates.

Overview of VBA Input Box with custom positions

Steps:

  • Open a new module, insert the code below, and run it.
Sub InputBox_Custom_Positions()
lookup_text = InputBox("Enter the employee name:", _
"Name Input", "Insert Name", 1800, 3000)
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 2, False)
MsgBox "The employee age is: " & result
End Sub

VBA code to customize position of the Input Box

  • You have the InputBox placed according to the given coordinates.

Input Box with customized position

  • To check whether the code is working, input a name and click OK.

Insertion of name to lookup from the datatable

  • You have the final result for the given name.

Final output with lookup value


Example 5 – Adding a Help Section to for an InputBox

The InputBox has limited options for customizing the appearance and behavior of the dialog box. A Helpfile is a separate document that provides additional information to the user.

Overview of adding Helpfile to create custom VBA Input Box

Steps:

  • Insert a module, insert the below code into it, and hit Run.
Sub InputBox_helpfile()
lookup_text = InputBox("Enter the employee name:", _
"Employee Information", "Insert Name", , , _
helpFilePath = "E:\Employee Data", HelpContextID = 10)
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 3, False)
MsgBox "The employee department is: " & result
End Sub
VBA code to include Helpfile inside the Input Box
  • Click the Help button to get instructions for filling out the input fields.

Input Box with Help button

  • A tab will be opened in your browser, since that’s the details of the file we’ve used.

Final output by clicking Help button


Example 6 – Inserting a VBA Input Box That Takes a Specific Data Type

When using an InputBox, it is often helpful to specify the expected data type of the input, such as a number, date, or string. By specifying the data type of the input, the program can validate the user’s input and ensure that it is in the correct format. This can help prevent errors or unexpected results when the input is used later in the program. Check out the list box below to learn about various specific data types.

Type Number Type Validation
0 Formula is acceptable
1 Numeric value acceptable
2 Text value acceptable
4 Logical value like TRUE or False acceptable
8 Cell reference acceptable
16 Error values like #N/A, #DIV/0 acceptable
64 Array of values is acceptable

We get an error due to putting text input, as we have defined a numeric data type in the code.

Overview of Inserting Input Box with Specific Data Type


Case 1 – Take Numeric Value Only

  • Open a new module and insert the code with a numeric value of 1 as an acceptable data type.
Sub InputBox_Specific_Data()
input_value = Application.InputBox("Insert William’s Salary:", _
"User Information", , , , , , 1)
End Sub
  • Press the F5 key to run the code.

VBA code for specific data type

  • You get an InputBox. We will put a text value to check whether it’s working.

Insertion of text value to search among the data table

  • We have a warning message as we have inserted a text value.

Output with a warning message for putting text data type

  • If we put a numeric value, the code will run appropriately.

Insertion of numeric value to search from the dataset


Case 2 – Take Only Strings

  • Change the data type to 2 and run the code by clicking Run.
Sub InputBox_Specific_Data()
input_value = Application.InputBox("Insert employee name:", _
"User Information", , , , , , 2)
MsgBox "The employee name is: " & input_value
End Sub

VBA code for specific data type

  • Put a name and press OK.

Insertion of text value to search from the data table

  • We have the expected output with the proper specific data type.

Final output with the lookup value


Example 7 – Customizing an Input Box with Multiple Lines

You can also customize your InputBox with linefeed and carriage return. Check the table below for the constants you can use.

Constant Equivalent Chr Function Description
vbLf Chr(10) Linefeed
vbCr Chr(13) Carriage return
vbCrLf Chr(13) and Chr(10) Both Carriage return and linefeed
vbNewLine Chr(13) and Chr(10) for Windows
Chr(13) for mac
New line character for specific platform

Using the vbNewLine adds a new line just below the prompt line.

Overview of customizing Input Box with multiple lines

Steps:

  • After inserting a module, insert the code below and run it.
Sub InputBox_Multiple_Line()
lookup_text = InputBox("Enter the employee name:" & vbNewLine & _
"(From the employee records in the worksheet)")
Set Rng = Range("B5:E14")
result = WorksheetFunction.VLookup(lookup_text, Rng, 2, False)
MsgBox "The employee age is: " & result
End Sub

VBA code for inserting multiple lines inside Input Box

  • You get a new line just below the prompt string.
  • Put a name inside the box and press OK.

Input Box with a new line

  • You get the output.

Final result with lookup value for the inserted name


How to Create a Custom Input Box with Multiple Inputs in Excel VBA

Here’s an example of an InputBox that accepts multiple input values.

Overview image for creating a vba custom Input Box with multiple inputs


Step 1 – Creating the UserForm Window

  • Launch the Visual Basic window >> click on Insert >> select UserForm.
  • Insert Labels using the ToolBox and change the name of Labels using the Caption option from the Properties window.
  • Change the Font style and size to match your choice.
  • Change the Height, Left, and Width values to 20, 30, and 80 to position the labels.
  • Selecting all the Labels, change the BackColor to light green using the BackColor code- &H0080FF80&.
  • Insert 3 TextBoxes and 1 ComboBox for collecting values.
  • Place them inside a Frame and name them Employee Form.
  • Create 3 CommandButton for submitting, resetting, and canceling input values.
  • In a new worksheet, define the name Department for the department name and combine it using the RowSource option.

Step 2 – Writing the Code and Running the UserForm

VBA code for CommandButtons

  • Use the below code for all three CommandButtons.

Submit Button:

Private Sub CommandButton1_Click()
Worksheets("MultipleInputs").Activate
i = 1
While Range("B4").Cells(i, 1) <> ""
i = i + 1
Wend
MsgBox i
Worksheets("MultipleInputs").Range("B4").Cells(i, 1) = UserForm1.TextBox4.Text
Worksheets("MultipleInputs").Range("C4").Cells(i, 1) = UserForm1.TextBox5.Text
Worksheets("MultipleInputs").Range("D4").Cells(i, 1) = UserForm1.ComboBox2.Value
Worksheets("MultipleInputs").Range("E4").Cells(i, 1) = UserForm1.TextBox6.Text
End Sub

Reset Button:

Private Sub CommandButton2_Click()
TextBox4.Value = ""
TextBox5.Value = ""
ComboBox2.Value = ""
TextBox6.Value = ""
End Sub

Cancel Button:

Private Sub CommandButton3_Click()
Unload Me
End Sub
  • Run the UserForm. You successfully created a custom InputBox with multiple inputs.

How to Check If the Cancel Button Was Clicked in an Input Box

Method 1 – Employing the Input Box Function

We need a box where a Cancel button will be included. Besides the null result, we’ll put a message if the user clicks the Cancel button.

Steps:VBA code to include Cancel button

  • Put the code and run it by hitting the Run
Sub InputBox_Cancel_button()
Input_Name = InputBox("Insert Employee name", _
"Employee Information")
If Input_Name = cancel Then
MsgBox "You clicked Cancel."
Else
MsgBox "You entered: " & Input_Name
End If
End Sub Clicking Cancel button to get the output
  • After the InputBox appears, press Cancel.

Final output for clicking Cancel button

  • A MsgBox will appear confirming the user clicked the Cancel button.

Method 2 – Using the Application.InputBox Method Without Selecting a Cell Range

Steps:

VBA code for selecting cell range inside an InputBox

  • Place the code below and Run.
Sub inputbox_cell_range()
Dim cell_range As Range
On Error Resume Next
Set cell_range = Application.InputBox("Select a range", Type:=8)
On Error GoTo 0
If cell_range Is Nothing Then
MsgBox "You clicked cancel."
Else
MsgBox "Your selected range is: " & cell_range.Address & "."
End If
End Sub
Input Box with selected cell range from the data table
  • An InputBox will appear where you can choose your desired cell range.

Result with chosen cell range from the data table

  • A MsgBox with the chosen cell range will be displayed.

Clicking the Cancel button to get the output

  • Without selecting the cell range, click the Cancel button.

Final output for pressing the Cancel button from the custom Input Box

  • A MsgBox will confirm that you pressed the Cancel button.

Things to Remember

  • The application method is mostly used to set the input data type. If it is not used, then try to provide a specific data type.
  • Like other functions, InputBox can accept up to 255 arguments.

Frequently Asked Questions

What are the pros and cons of using Excel VBA Custom InputBox?

A custom InputBox in VBA offers several advantages, such as improved user experience, data accuracy, and customization options. However, there are also potential drawbacks, such as confusing interfaces, time-consuming development, restrictive validation checks, and compatibility limitations.

How do I retrieve the input values from a custom InputBox?

To retrieve the input values from a custom InputBox, you need to reference the values of the controls on the form. For example, if you have a text box for the user to enter their name, you can retrieve the value of the text box using the Text property.

How do I handle validation and error checking in a custom InputBox?

You can handle validation and error checking in a custom InputBox by using conditional statements and an error handling code. For example, you can check if the user has entered a valid email address before allowing them to submit the form, or you can display an error message if the user leaves a required field blank.


Download the Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Wasim Akram
Wasim Akram

Wasim Akram holds a BSc degree in Industrial and Production Engineering from Ahsanullah University of Science and Technology. Over the past 2 years, he has been actively contributing to the ExcelDemy project, where he has authored more than 150 articles. Now, he is working as an Excel VBA and Content Developer. He likes learning new things about Microsoft Office, especially Excel VBA, Power Query, Data Analysis, and Excel Statistics. He is also very interested in machine learning and... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo