The Wayback Machine - https://web.archive.org/web/20240926235837/https://www.geeksforgeeks.org/how-to-create-an-input-box-with-multiple-inputs-in-excel-using-vba/
Open In App

How to Create an Input Box With Multiple Inputs in Excel Using VBA?

Last Updated : 28 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The Input Box is a dialogue box that helps users to take a value and do later computations according to the entered value. The input box is similar to the message box, but the message box is used to display the data while the input box is used to enter the data. By default, a message box displays only the Ok button in its conversation box, whereas an input box displays both the Ok and Cancel buttons. You can specify the type of data to be returned.

In excel VBA Macro, Input Box is one of the most commonly used functions. In this example, we will learn how to create an input box using excel VBA which consumes multiple data from the end-user. For this, we will be creating a user form.

Step By Step Implementation

Step 1: Insert Visual Basic Editor

To insert Visual Basic Editor in excel please refer to How to Insert and Run VBA Code in Excel?

Step 2: Insert User Form

In this step, we need to add a user form to our VBA. For this, we need to go to the Developer tab on the top of the ribbon and then select Visual Basic.

Open visual basic

Fig 1 – Open Visual Basic

Once we click on the Visual Basic option, excel will open Visual Basic Editor in a new window.

Visual Basic Editor opened

Fig 2 – Visual Basic Editor

In the Visual Basic Editor, we need to insert user form for this we just need to click on the Insert UserForm option.

Insert UserForm

Fig 3 – Insert UserForm

Once we click on Insert UserForm, excel will automatically insert a user form.

User Form created

Fig 4 – User Form

If the Toolbox window doesn’t get open automatically, then Click on View and then Toolbox. It will open the toolbox window. 

Step 3: Adding Labels to User Form

In this step, we will add the required labels we need in our user form. For this example, we will be using the following labels in our user form.

Labels Name Caption
Frame GeeksForGeeks GeeksForGeeks
TextBox Name Name
TextBox Age Age
ComboBox Branch  
CheckBox Technical Content Writer  

First, we need to add Label to our User Form. In order to add a label, we go to Toolbox and then drag the Label to User Form.

Adding Label to User Form

Fig 5 – Adding Label to User Form

Once we add Label to our User Form, we need to format it using the Properties Window.

Note: If by default Properties Window is not visible, we can add it. For this Select Label then View and then Properties Window.

Now, we will format our label, for this, we need to Select Label then Properties Window, and then Caption & Text Align.

Changing Caption to name & TextAlign

Fig 6 – Changing Caption & TextAlign

We will also change the background color for this Select Label then Properties Window then BackColor here we choose Palette from Choose Color.

Changing BackColor from properties.

Fig 7 – Changing BackColor

Now, we need to copy this Label for all of our User Form fields. For this Select Label and then copy with Ctrl+C and then paste it with Ctrl+V,  all the labels below one another.

Copying Labels for All the Fields

Fig 8 – Copying Labels For All Fields

Once, we have copied and pasted the required labels we need to change the field’s name according to our requirements. For changing fields name we will use the Properties Window as we have done above. Once we update all the labels as per our requirement, we will get the following output.

Changin labels of fields.

Fig 9 – Changing Labels

Before moving further, we can align our labels. For this Select Labels and then choose Format on the top of the ribbon then select Align and choose Left. This will align all the Labels to left in a straight line.

Note: To select all labels, we need to drag our cursor over all the labels.

Align All Labels to Left.

Fig 10 – Align All Labels to Left

Step 4: Adding Input Box

In this step, we will add the required fields to take input from user. Here, we will use TextBox for Name and Age. For Branch we will be using ComboBox and for the Technical Content Writer field we will be using CheckBox.

First, we will add TextBox for Name Field. For this go to Toolbox and then drag the TextBox to UserForm.

Adding TextBox.

Fig 11 – Adding TextBox

Similarly, we will add TextBox for Age Field. Also, we will add SpinButton along with TextBox for age field.

Adding SpinButton for age field.

Fig 12 – Adding SpinButton

Now, For Branch Field we will add ComboBox. In order to add this we need to go to Toolbox and then drag the ComboBox to User Form beside branch field. ComboBox is used for adding list elements.

Adding ComboBox for Branch field.

Fig 13 – Adding ComboBox

Finally, for our Technical Content Writer field we will be using CheckBox. For this go to Toolbox and then drag the CheckBox to UserForm.

Adding CheckBox for technical content writer field.

Fig 14 – Adding CheckBox

We need to change the caption for our CheckBox. For this Select CheckBox and then in Properties Window click Caption. Here, we are changing it to “Yes”.

Changing Caption

Fig 15 – Changing Caption

Step 5: Adding Frame

Now, we need to add our user form in a frame. For this, we need to go to Toolbox and then drag the Frame to User Form. But if we drag the frame over our user form, it gets overlapped on our fields and attributes. So, First We need to select all the fields and attributes by dragging over the user form and then copy it. After that we will delete everything and add frame to our user form and then paste the copied fields and attribute.

Note: Make sure you have copied everything from user form, before deleting it.

Select Everything & Copy It

Fig 16 –  Select Everything & Copy It

Once we have copied everything to clipboard, we need to delete all of them and add frame to our user form.

Adding Frame

Fig 17 – Adding Frame

After we have added the frame, we need to paste all the clipboard items that we have copied and will get the following output.

Final Output

Fig 18 – Output

Now, we have added frame to our user form. We can also format the Frame as per our requirement using the Properties Window. Here, we are changing the Captions for frame as “Fill the Form“ and for UserForm as “GeeksForGeeks“.

Adding Caption to Frame & User Form

Fig 19 – Adding Caption To Frame & User Form

Step 6: Adding Button

In this step, we will add buttons to our user form. For adding button, we go to Toolbox and then drag the  CommandButton to User Form. 

Adding Command Button.

Fig 20 – Adding Command Button

Now, we will change the text of button using Caption from Properties Window. We will change it as per requirement we will get following output.

After button Output

Fig 20 – Output

Before moving ahead, we can run our VBA code. To run VBA code please refer How to Insert and Run VBA Code in Excel? For this Select User Form and then Run Button in Menu.

To Run User Form

Fig 21 – To Run User Form

Once we run our user form, we will get the following output.

Running User Form

Fig 22 – Running User Form

In above user form, we can add text to our Name Field. But for Age and Branch we need to define their functionalities. 

Step 7: Adding Age Functionality

In this step, we will add functions to our Age Fields. When we click on our age field textbox we will get the name of textbox that we need to link to SpinButton. So, that age gets updated whenever we click on spin button.

Age Field Name

Fig 23 – Age Field Name

As above, we can see age field name is TextBox2. Similarly, we will check spin button name. After that we need to link the spin button with age field name. For this Double-click on Spin Button. It will open VBA code editor. Where we need to write the following code.

TextBox2.Value = SpinButton1.Value

We will also set min and max value for age field. Here, we are using min value as 18 and max as 62.

Link Spin Button with Age Field

Fig 24 – Link Spin Button with Age Field

Once we have added the code in VBA Editor. We will run our user form and will use spin button to change the value.

Age Field

Fig 25 – Age Field

Step 8: Adding Branch Functionality

In this step, we will add function to our Branch Field. For this, we need list of branches (CSE, Mech, IT, Electrical, Chemical). We will create a new sheet in excel and add list of the branches to it.

Branch List in excel.

Fig 26 – Branch List

Now, we need to give a name to the Range of list. For this Select All Row and then Add Name in Name Box. Here, we are using range name as branch_list.

Range Name

Fig 27 – Range Name

After this, we need to link the range name of our branch list to our branch field combobox. For this Select ComboBox (Branch Field) then in Properties Window select Row Source and add the range name there.

Adding Range Name to RowSource

Fig 28 – Adding Range Name To RowSource

After this when we run our VBA Code, we will get the list of branch visible in the drop-down menu.

Branch List in form

Fig 29 – Branch List

Before moving further, we need to add columns for our fields in the excel sheet. Which will store data entered by users in our user form.

Adding Columns to Excel

Fig 30 – Adding Columns To Excel

Step 8: Adding Submit Button Functionalities

In this step, we will add function to our Submit Button. For this we need to do Double-Click on Submit Button then in VBA Code Editor click Add Code. We will be adding the following code in VBA code editor. Make sure that you have changed the cell & fields name according to your excel sheet.

Sheet1.Activate

Range(“A1”).End(xlDown).Offset(1, 0).Value = TextBox1.Value

Range(“B1”).End(xlDown).Offset(1, 0).Value = TextBox2.Value

Range(“C1”).End(xlDown).Offset(1, 0).Value = ComboBox1.Value

If CheckBox1.Value = True Then

Range(“D1”).End(xlDown).Offset(1, 0).Value = “Yes”

Else

Range(“D1”).End(xlDown).Offset(1, 0).Value = “No”

End If

Unload Me

Submit Button Function

Fig 31 – Submit Button Function

After we add function to our submit button. We will run our user form and enter the data. The data gets stored into the excel sheet.

User Form Data Storing

Fig 32 – User Form Data Storing

Step 9: Adding Cancel Button Functionalities

In order to add cancel function. We will need to write the following code. It will remove our user form from the foreground.

Unload Me
Cancel Button Function

Fig 33 – Cancel Button Function

Step 10: Adding Reset Button Functionalities

In this step, we will add function to our reset button. We will be using following code. It will make all fields to blank(empty).

TextBox1.Value = ""
TextBox2.Value = ""
ComboBox1.Value = ""
CheckBox1.Value = False
Reset Button Function

Fig 34 – Reset Button Function

Step 11: Adding Macros for User Form

In this step, we will add a macro to access our user form. Every time the end user need not to go to developer option and access our user form. We will add a control command button on clicking user will access user form. For this go to Excel Sheet and then in Developer tab on the top of the ribbon and then in Enable Design Mode group select Insert and then drag the Command Button (ActiveX Control) to Excel Sheet.

Adding ActiveX Control: Command Button

Fig 35 – Adding ActiveX Control: Command Button

After we inset the Command Button, we need to add function to it. So that, whenever any user clicks on the button, it will open our user form. To add functions, it we need to Double-Click Command Button and Add Functions. Here, we are also formatting our command button using Properties Window.

Adding Function to Command Button

Fig 36 – Adding Function to Command Button

After this, we need to disable the Design Mode and Click Command Button (ActiveX Control). It will open our user form.

Step 12: Output



Similar Reads

VBA Subroutine in Excel - How to Call Sub in VBA?
When a specified action is performed on a worksheet with the help of a collection of code known as a VBA Subroutine. It also helps to read an external file, also it can open other applications from Excel. A large piece of code can be broken into small parts so that we can manage it easily. Let's learn why to use submarines: Converts large piece of
2 min read
How to Mask Password in VBA Excel Input Box?
A combination of integers, strings of characters, or other symbols is used to verify the identity of a user during the authentication process so, that they can access the system. But a password should always remain in a hidden format to prevent it from shoulder spying. Shoulder spying is a practice where a person collects our personal data by spyin
2 min read
How to Convert Multiple PowerPoint Files Into Pdf with Excel VBA?
Often clients need PPT files as PDFs. It helps to view on any device. Use below VBA Macro to convert PowerPoint files from a folder and save them as PDF in the same folder. Implementation: Follow the below steps to convert multiple PowerPoint files into PDFs using Excel VBA: Step 1: Open Excel. Step 2: Type text “Folder Path” in cell “B5” (Image 1)
2 min read
VBA Multiple (Nested) If Statements in Excel
VBA in Excel stands for Visual Basic for Applications, which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. What decision-making reasoning do you often use in your Excel worksheets? In most circumstances, you would test your condition using an IF
5 min read
How to Create Charts in Excel Using Worksheet Data and VBA?
Excel is an important software provided by Microsoft Corporation. This software belongs to one of the major software suites Office 365. In this software suite, there are other software are present like Word, PowerPoint, etc. They are called Office 365, as this software are mostly used for office purpose. But now the world has changed a lot. After t
6 min read
How to Create Dynamic Range Based on Cell Value in Excel VBA?
Dynamic ranges in excel VBA provide us with the flexibility to define ranges beyond the specific values. The dynamic range enables the program to perform transformation and data management steps with ease without the worry of changing the range of the data set. The dynamic range in excel VBA promotes code reusability and does not make the programmi
3 min read
How to Create a User Defined Function in Excel VBA
A function is a collection of code.  As a developer, we very often need a custom function (User-defined function) in our projects. These functions can be used as normal functions in Excel. These are helpful when the existing functions are not enough. In such cases, the user can create his own custom user-defined function to fulfil the need.  What a
6 min read
How to Remove Duplicates From Array Using VBA in Excel?
Excel VBA code to remove duplicates from a given range of cells. In the below data set we have given a list of 15 numbers in “Column A” range A1:A15. Need to remove duplicates and place unique numbers in column B. Sample Data: Cells A1:A15 Final Output: VBA Code to remove duplicates and place into next column (B) Declare Variables: VariablesData Ty
2 min read
Excel VBA | count() functions
Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its var
2 min read
How to Use for Each Loop in Excel VBA?
A For Each loop is used to execute a statement or a set of statements for each element in an array or collection. Syntax: For Each element In group [ statements ] [ Exit For ] [ statements ] Next [ element ] The For...Each...Next statement syntax has the following three parts: PartDescriptionelement Required (Must be mentioned). Variable is used to
3 min read
Excel VBA | sum() functions
Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its var
2 min read
Excel VBA | Average() Functions
VBA (Visual Basic for Applications) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA, one can automate many tasks in excel and all other office softwares. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its v
2 min read
How to Declare and Initialize String Array in Excel VBA?
A string array is an array where we can store only string values in the array, with the help of a string array, we can store more than one string value. We can declare the string array in many ways like declaring a static string array, declaring a variant size array of string using the Array function, and a string array using the split function whi
2 min read
Trapping Dynamic Ranges in Excel VBA
Dynamic Ranges give the flexibility to work with more range of cells as compared to a specific range of cells which usually limits us to work with a particular range of cells. As the size of the data may change in an excel sheet so it is also necessary to change the range in the code dynamically for that dynamic ranges are used. We can trap dynamic
3 min read
How to Use the VBA Immediate Window in Excel?
The immediate window is similar to the console in Chrome, where we could execute the javascript. The immediate window allows us to execute macro code very fast and efficiently. Once, you learned it, you will always be found using it. Your immediate window can answer an infinite number of questions, like setting a cell value, changing the sheet of t
5 min read
How to Set Variable to Cell Value in Excel VBA?
Adding data to the worksheet is often done and it is essential to learn how to set variable to cell value in excel VBA. There can be instances when you want to add the same data to your worksheet. This task could easily be achieved with the help of Excel VBA variables. You can assign your cell values with variables. Let's learn how to set variables
5 min read
Excel VBA Concatenation Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Concatenation means to join two or more data into a single data. There are various ways we can perform concatenation in Excel using built-in
6 min read
How to Use Select Case Statement in Excel VBA?
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to discuss how to use Select Case Statement in Excel VBA. Select Case Statement of Excel VBA The select case i
10 min read
Excel VBA Comparison Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Some helpful links to get more insights about Macros, VBA in Excel : 1. Record Macros in Excel. 2. How to Create a Macro in Excel? In this a
5 min read
How to use If-Else Statement in Excel VBA?
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. In this article, we are going to learn how to use the If Else statement in Excel VBA. Implementation : In the Microsoft Excel tabs, select t
2 min read
How to use If-Else-If Statement in Excel VBA?
In this article, we are going to look into how to use the If Else If statement in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step process : Right-click on any of the existing tabs at th
3 min read
How to use While Wend Loop in Excel VBA?
In this article, we are going to see about While Wend loop in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step process : Right-click on any of the existing tabs in the top of the Excel w
2 min read
How to use Do Until Loop in Excel VBA?
In this article, we are going to see look into the Do Until loop in Excel VBA using a suitable example. Implementation : In the Microsoft Excel tabs, select the Developer Tab. Initially, the Developer Tab may not be available. The Developer Tab can be enabled easily by a two-step process : Right-click on any of the existing tabs in the top of the E
3 min read
How to Add a Comment in a VBA in Excel?
VBA Macro is for developers. Macro is a piece of code written in VBA. VBA is Microsoft's programming language and it stands for Visual Basic for Applications. Let's see how to set up our VBA Macro and how to add comments in a VBA in Excel. VBA Macro Excel is a very advanced tool that contains thousands of functionalities, but VBA Macro comes into e
4 min read
How to Use Do While Loop in Excel VBA?
A Do…While loop is used when we want to repeat certain set of statements as long as the condition is true. The condition may be checked at the starting or at the end of the loop Flowchart: Uses of Do-While loop: The Do While loop is used in two ways: Do…while loop which checks the condition at the STARTING of the loop.Do…while loop which checks the
2 min read
How to Use For Next Loop in Excel VBA?
If you are familiar with the programming you must have an idea of what a loop is, In computer programming, a loop is a sequence of statements that are repeated until a specific condition is satisfied. In Excel VBA the "For Next" loop is used to go through a block of code a specific number of times. Syntax: For counter = start to end [step count] [c
4 min read
How to Find the Last Used Row and Column in Excel VBA?
We use Range.SpecialCells() method in the below VBA Code to find and return details of last used row, column, and cell in a worksheet. Sample Data: Syntax: expression.SpecialCells (Type, Value) Eg: To return the last used cell address in an activesheet. ActiveSheet.Range("A1").SpecialCells(xlCellTypeLastCell).Address VBA Code: Declaring Variables:
2 min read
How to Get Length of Array in Excel VBA?
We use UBound and LBound functions to get the length of an Array in Excel VBA. In this article, we will discuss them in detail. Syntax: UBound() function UBound (arrayname, [ dimension ]) Parameters: arrayname: required. Array variable namedimension: optional Returns: Return upper limit of an array dimension. Syntax: LBound() Function LBound (array
2 min read
How to Delete a Module in Excel VBA?
Have you ever seen a text file in a notepad or a source code file in visual studio code? These all are just basic files where you can write some code or information. Similarly, we have modules in excel, each module has its code window, where you can write some code and allow it to automate your repetitive tasks. There can be multiple modules. We ca
3 min read
How to Run Code from a Module in Excel VBA
VBA Macro is for developers. In Excel, the macro is a piece of code written in VBA and VBA is Microsoft's programming language, it stands for Visual Basic for Applications. The module is a file with a .bcf extension that stores the code written in the Visual Basic for Applications editor. Let's learn, how to run a code from a module in Excel VBA. R
4 min read