Open In App

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

Last Updated: 28 Jan, 2023

A

Summarize
Comments
Improve
Suggest changes
Like Article
Like
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



A

News
Improve
Discuss
Do you want to advertise with us?Click here to know more

VBA Subroutine in Excel - How to Call Sub in VBA?

article_img
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 codes into small parts so that the computer ignores all kind of complexities that arises because of large codesReusability of code suppose we in a program have to access the database frequently so instead of writing the code again and again we can create a function to access the databaseSubroutines are self-documenting functions which means a coder can easily say what the program does by looking into the name of the function Naming Rules of SubroutinesIt can start with a letter or an underscore but it cannot start with a number or a special character.It cannot contain any space in the name.The name of the subroutine cannot be a keyword like Private, Sub, End, etc. Syntax Private Sub function_name( ByVal arg1 As String, ByVal arg2 As String) End Sub Syntax Explanation Code Action "Private Sub function_name(...)"Private is the keyword whic
Read More

How to Mask Password in VBA Excel Input Box?

article_img
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 spying over our shoulders in Cyber Cafe or ATM. Let's learn how to hide the password for that we need to mask the password in Excel Input Box. Steps to Mask Password in VBA Excel Input Box Follow the further steps to create a masked password in VBA: Step 1: Press Alt+F11 in the Excel window to get the VBA window. Step 2: Select Insert in the Ribbon tab of the VBA code window. Step 3: Then click on UserForm to get the toolbox and user form. Step 4: Select CommandButton from Toolbox. And then drag it to the UserForm1 tab. Step 5: Select TextBox from the toolbox and then drag it to UserForm1. Step 6: Press F4 to get the Properties Window. Step 7: Change the name of the Text Box to "txtPassword" Step 8: Now, scroll down the Properties tab of txtPassword to assign a value of asterisk(*) in PasswordChar to create a password mask. Step
Read More

How to Convert Multiple PowerPoint Files Into Pdf with Excel VBA?

article_img
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). Step 3: Enter your folder full path in cell “C5” (Image 1). Here we have a folder “D:\Excel\29.ppt2pdf\ppt” with two PPT files (Image 2). Image 1Image 2 Step 4: Write below VBA code in your VBE module Sub ppt2pdf_Macro() Dim oPPTApp As PowerPoint.Application Dim oPPTFile As PowerPoint.Presentation Dim onlyFileName As String, folderPath As String, pptFiles As String, removeFileExt As Long Application.ScreenUpdating = FalseInitialize variablesfolderPath = Range("C5").Text & "\" pptFiles = Dir(folderPath & "*.pp*")Check and exit macro if no ppt files are in the folderIf pptFiles = "" Then MsgBox "No files found" Exit Sub End If Do While pptFiles <> ""Assign PowerPoint application to variable Set oPPTApp = CreateObject("PowerPoint.Application") oPPTApp.Visible = msoTrue On Error Resume NextAssign PowerPoint presentation to
Read More

VBA Multiple (Nested) If Statements in Excel

article_img
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 formula and return one value if the condition is met and another value if it is not. You can nest multiple IFs inside of one another to assess more than one condition and return different values based on the outcomes. Where to Put the VBA Code in Excel?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 the top of the Excel window.Now select Customize the Ribbon from the pop-down menu. In the Excel Options Box, check the box Developer to enable it and click on OK. Now, the Developer Tab is visible. Now click on the Visual Basic option in the Developer tab and make a new module to write the program using the Select Case statement.Developer  -> Visual Basic -> Tools -> Mac
Read More

How to Create Charts in Excel Using Worksheet Data and VBA?

article_img
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 the Corona Pandemic, the world knows the positivity of using digital tools. Office 365 was not different from that. As a part of the software suite, Excel software also gains some importance from the users. They are not only used for official purposes. But they can also be used for school purposes. Excel is software that can able to store data in an effective form. So, searching for the data becomes more manageable in this software. Excel has another great feature. It can be used to derive the charts from the provided data. The charts are helpful for analyzing any growth of the data. If there are thousands of data present, it is a difficult task to extract some analysis from that data. But if those data are converted to charts, then it will be easy to analyze those data. Excel sheet helps to do the same. Charts can be prepared whatever the
Read More

How to Create Dynamic Range Based on Cell Value in Excel VBA?

article_img
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 programming logic to be redundant. By utilizing the range object and cells object, the dynamic ranges in Excel VBA can be defined and created. There are many ways in which dynamic ranges can be created. Following are the methods listed below: Variable initialization to the range and cell object.Create a Dynamic range passed as the value in another excel cell. The following examples help in illustrating the above methods, Variable Initialization to Range and Cell Object With the change in the size of the data, it would be useful if the range object that is used to refer to the ranges can be modified. The following code helps in counting the last row and column in an excel as shown below Sub test() Dim EndR As Integer Dim lastCl1 As Integer Sheet2.Activate EndR = Sheet2.Range("A1").End(xlUp).Row lastCl1 = Sheet2.Range("XFD1").End(xlToLeft).Column Ms
Read More

How to Create a User Defined Function in Excel VBA

article_img
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 are User Defined FunctionsUser Defined Function(UDF) is the custom function that is created by the user to perform a specific task in VBA(Virtual basic application) which is a programming language in Excel. Function Vs. Subroutine in VBA In VBA, a 'Subroutine' lets you run a group of commands, while a 'Function' provides a result. For example, consider a list of numbers, some positive and some negative. With a subroutine, you can scan each cell and mark negative ones by changing their colour. The subroutine modifies the cell properties. On the other hand, a custom function can be used in a separate column. It returns TRUE for negative values and FALSE for positive ones. Functions can't change cell properties directly but can be used with conditional formatting for the same effect. When you create a User Defined Function (UDF) in VBA, you c
Read More

How to Remove Duplicates From Array Using VBA in Excel?

article_img
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 Sample Data Final Output: VBA Code to remove duplicates and place into next column (B) Declare Variables: VariablesData TypeCommentsnonDuplicateBooleanIt is a Boolean value (True/False).uNoIntegerCount no of Unique items in column BcolAIntegerIteration column A cellscolBIntegerIteration column B cells'Variable Declarations Dim nonDuplicate As Boolean, uNo As Integer, colA As Integer, colB As Integer Always first value will be unique, So A1 place to cell B1 'Place first value to B1 Cells(1, 2).Value = Cells(1, 1).Value Initialize variables: 'Initialize uNo = 1 since first number is already placed in column B; Assign True to the variable nonDuplicate uNo = 1 nonDuplicate= True Since the first number is already placed in cell B1, Loop starts from A2 to A15. Take each number from Column A and check with Column B (unique range) 'Use for loop to check each number from A2 to A15 For colA = 2 To 15 For colB = 1 To uNo if the number is already placed in
Read More

How to Use Select Case Statement in Excel VBA?

article_img
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 in VBA is like a detective that investigates an expression by checking it against different scenarios listed as Case Statements, each with its own conditions. When a scenario matches the expression, it's like solving a piece of the puzzle, and the corresponding code linked to that scenario is activated. Importantly, once a match is found, the detective stops investigating and moves on to executing the discovered code. Yet, if none of the scenarios match, it's like the detective hitting a dead end. In this situation, the code associated with the Case Else statement comes into play, offering a default solution or outcome. Syntax of the Select Case Statement of Excel VBAThe VBA Select Case Statement shares similarities with the Switch Case construct found in programming languages such as Java, C#, and PHP. In Excel VBA, this statement helps
Read More

Excel VBA Concatenation Operators

article_img
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 functions, operators, etc. Some helpful links to get more insights about concatenate and using VBA in Excel : Record Macros in ExcelCONCATENATE in ExcelHow to Create a Macro in Excel? In this article, we are going to see about concatenate operators and how to use VBA to concatenate strings as well as numbers. 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 the top of the Excel window.Now select Customize the Ribbon from the pop-down menu.In the Excel Options Box, check the box Developer to enable it and click on OK. Now, the Developer Tab is visible. Now, we need to open the Visual Basic Editor. There are two ways : Go to Developer and directly click on the
Read More
three90RightbarBannerImg