How to Insert and Use Excel/VBA Textbox

VBA text box tutorial showcasing techniques for implementing and customizing text boxes in Visual Basic for Applications programming.

Text box ActiveX control in Excel

In this tutorial, you will see how to insert text box ActiveX control in an Excel sheet.

We will also show how to write VBA code to fill text in the text box control.

How to insert text box control in an Excel sheet?

Follow these steps to insert a Text Box control into an Excel sheet:

Step 1:

In the “DEVELOPER” tab, locate the “Insert” button group:

VBA developer tab location

Step 2:

There, look for the “Text Box” control in the Insert button group, click it:

Excel textbox location

Step 3:

Add the textbox in the Excel sheet by “drag-drop” the mouse as per the required size:

Excel textbox add

Writing text in the Text box

If you want to write text in the Text box while in the Excel sheet, just “Unselect” the “Design Mode

Excel design mode unselect

Now you will be able to write text in the text box as you click inside it.

Writing text by using VBA code

In this example, we will write text into the Text box as you click the Command Button (ActiveX control).

For that, we need to write the VBA code in the click event of the button.

Step 1:

First of all, add a button and a text box in the sheet:

VBA textbox with button image

To learn more about buttons and how to change their captions, go to its tutorial: VBA/Excel Command Button

Step 2:

Ensure, “Design Mode” is active. Double-click on the button to open the VBA code editor:

(You may also right-click on the button and press the “View Code” option to open the VBA editor)

Step 3:

Write the following line of code in the click event of the button:

TextBox1.Text = “Testing VBA test box control”

Save the code and get back to the Sheet.

Step 4:

Click on the button and you will see the Text box is populated with the text we used in the VBA code:

VBA textbox written

(Ensure that “Design Mode” is unselected to execute the code)

Assign the text box’s text to a cell example

In this example, we have written some text in our Textbox. The task is to assign that text to the B5 cell as we click on the button.

Step 1:

Write some text after adding the Text Box control in the Excel sheet. Also, add a Command Button control in the sheet:

VBA textbox buttton

Step 2:

Double-click on the button to open the Code editor and write this line of code:

Range("B5").Value = "Write this text to B5 Cell"

Save the code and get back to the sheet.

Step 3:

Click on the button and you should see the Text box’s text is assigned to the B5 cell:

VBA textbox cell

Write same text in the Sheet cell as in the Text Box typed

In this example, we will update the text in the A3 cell live i.e. as you type in the textbox, the text in the cell will also be changed to the same text as in the textbox control.

For that, we will write code in the textbox’s TextBox_Change() event.

Follow these steps.

Step 1:

Add a textbox in the sheet and double-click on it to open the editor.

It should automatically take you to the TextBox_Change() event code section.

Step 2:

Write this line of code:

Range("A3").Value = TextBox1.Text

 

VBA textbox live edit

Save the code and get back to the sheet.

Step 3:

Start typing in the textbox after unselecting the “Design Mode” and you will see the text is also updated on the A3 cell:

VBA textbox live udpate

This happens because the change event occurs as you type in the textbox. Every time you enter/delete or update some text in the textbox, the line of code executes that changes the text in the A3 cell as well.

Writing to more than one cells

Similarly, you may write to many cells by providing a range of cells to the VBA range object rather than just one cell.

Just replace the above example’s code with this and the same text will be written to A2 to A7 cells:

Range(“A2:A7”).Value = TextBox1.Text

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we solve the mysteries of coding together!