What Is a VBA TextBox?
A TextBox is a box that receives data. It is a part of the UserForm.
To insert a TextBox in a VBA UserForm:
Steps:
- Hold Alt + F11 to open the Microsoft Visual Basic Applications window.
- Click Insert and select UserForm.
The UserForm is created.
- Create a TextBox.
- A “+” icon is displayed. Drag it to create the TextBox.
Read More: How to Hide Textbox Using Excel VBA
Properties of a VBA TextBox in Excel
- Press F4 to display the properties of the TextBox.
Properties include Name, Color, Border Color, Border Style, Cycle, Font, Height, Picture Alignment, ScrollBars, Tag, Width, Zoom, and many others.
Here, Name and Caption were changed.
Using the VBA TextBox Properties in Excel
Step 1- Select the Dataset
This is the sample dataset.
Step 2 – Insert a Label and a TextBox in Excel VBA
- Select Label in the Toolbox to draw a Label in the UserForm.
- Insert a TextBox and change the Label name.
- In TextBox Properties, change the name of the TextBox.
- Draw one more Label and a TextBox.
- Name the Label as Location and the TextBox as EmployeeLocationTextBox.
- Go to the Toolbox and select CommandButton.
- Change the name of the button to Submit.
- Press F5 key to run the UserForm.
Step 3 – Enter a VBA Code in the TextBox
- Go to the UserForm and double-click the Submit button.
An auto sub will be displayed.
- Enter the following code into the Visual Basic Editor.
Private Sub CommandButton1_Click()
Range("B5") = Me.EmployeeNameTextBox
Range("C5") = Me.LocationTextBox
Unload Me
End Sub
- Press F5 to run the code: the Employee Info dialog box will be displayed.
Step 4 – Enter Data in the TextBox
- Enter data and click Submit.
Data will automatically be inserted into the selected Range.
Read More: How to Add Text to Textbox Using Excel VBA
Final Output
- Change the Range of the VBA code to change the data input location. Repeat Step 4 to add more data.
This is the output.
Read More: How to Use VBA to Change Textbox Text in Excel
Download Practice Workbook
Download the workbook.
Related Articles
- How to Convert TextBox Value to Cell with VBA in Excel
- How to Make Textbox Datepicker with Excel VBA
- Excel VBA to Convert Textbox Value to Number
- How to Use Excel VBA Textbox Events
- How to Format TextBox for Phone Number in Excel VBA