VBA TextBox

Written by

Mel Jenkins

Reviewed by

Steve Rynearson

Last updated on September 6, 2021

This article will demonstrate how to use a Text Box in Excel VBA.

VBA has the ability to create forms that the user can interact with such as Invoice forms, or Contact details. Text Boxes are controls that can be used within these forms, or Text boxes can be created directly within Excel itself.

Creating Text Boxes on VBA forms

To insert a Text Box into a VBA form, we first need to create the form. This is done in the VBE Editor.

First, Open the VBE Editor in Excel.

To insert a new user form into your code, select the UserForm option from the Insert Menu.

vba vbe insert userform

A new user form will appear in the Project Explorer and will be shown in the Code Window on the right.

vba vbe userform

 

You can rename your form using the Properties box.  This should appear below your Project Explorer.

If it is how visible, Press F4 or click View, Properties Window.

VBATextBox Name Form

 

Click in the Name property and rename your form.

We can now start populating our form with controls – the text box control is the most popular control along with the label control.

To populate the form with controls, we need to switch on the Toolbox.

In the Menu, select View > Toolbox.

VBATextBox View Menu

To create a Text Box on your form, select the Text Box control in the Toolbox.

VBATextBox Toolbox

Drag a box shape on the form with your mouse, holding the left button down, and then release the mouse button.

VBATextBox DrawTextbox

 

VBATextBox TextBoxName

 

As this is the first Text Box we are creating, it will be named TextBox1.   As with the form, you can use the Properties window to rename the text box.

VBATextBox Rename TextBox

 

You might need to also create a label for your text box on the form.  This is done with a label control.  You drag on the form in the same way as you do for a Text Box, and then you can type the required text within the label control eg: First Name as shown below.

VBATextBox DrawLabel

 

To use the form within VBA Code, we would need to add a command button to the form.  This is done by selecting the Command Button control in the Toolbox and dragging the button onto your form.

VBATextBox DrawButton

You can then continue to create your form using textboxes, labels and command buttons, renaming them as appropriate.

VBATextBox Form

Writing Code Behind Forms

Once we have created our form, we need to write VBA code to insert the information in our form onto our Excel worksheet.  This code is called CBF (Code Behind Forms).

In the the VBE, double-click on the command button to go to the code behind the button.

VBATextBox Sub

A sub-routine for the click event for the button will be created.  We can type our VBA code in this routine.  For example:

Private Sub cmdOK_Click()
  Range("A1") = Me.txtFirstName
  Range("A2") = Me.txtSurname
  Range("A3") = Me.txtCellPhone
  Unload Me
End Sub

This routine will put the details typed in the text boxes into Excel, and then close the form.

VBATextBox EnterData

Creating Text Boxes in an Excel Worksheet

It is also possible to create text boxes within the Excel spreadsheet.  In order to do this, you need to have your Developer Ribbon switched on.

Note: If you don’t see the Developer Ribbon, you’ll need to enable it.

In the Ribbon, select Developer > Insert > ActiveX Controls > TextBox.

VBATextBox Ribbon

Click and drag in your Excel worksheet to draw the text box in the worksheet at the desired position.  Release the mouse button to complete drawing the text box.

VBATextBox ActveX

The text box will be in Design Mode.  While in this mode, we can size the box, and select the Properties of the text box.

VBATextBox DesignMode

Click on the Properties button to show the Property Window for the text box.  In this window, we  can change a variety of properties of the text box such as the name of the box, the color of the text, background or border, the border style and the font used for the text in the text box for example.

VBATextBox ActveX Properties

 

To use the text box direct in Excel as we have used it in the VBA form above, we need a way to return the data typed into the text box to the Excel sheet.  This can be done by adding a ActiveX command button to the worksheet, and using the click_event of this command button to return the information in the text box to the Excel sheet.

In the Ribbon, select Developer > Insert > ActiveX Controls > Command Button  to add a command button to your Excel sheet.   Switch on the Properties window and change the name of the button, the caption of the button and the Accelerator of the button as shown in the image below.

VBATextBox ActveX CmdButton

Double-click on the button to go to the VBA click event of the button and type the following code:

Private Sub cmdOK_Click()
   Range("A1") = Me.txtFirstName
End Sub

Switch back to the Excel worksheet, make sure Design Mode is switched off and then type in the created text box.

VBATextBox ActveX Click Event

Click the command button to return the text to Excel.

VBATextBox ActveX OK

 

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users! vba save as


Learn More!
vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples