Text Box in Excel VBA

 

A text box is an empty field where a user can fill in a piece of text. To create a text box in Excel VBA, execute the following steps.

1. On the Developer tab, click Insert.

2. In the ActiveX Controls group, click Text Box.

Create a text box in Excel VBA

3. Drag a command button and a text box on your worksheet.

4. Right click the command button (make sure Design Mode is selected).

5. Click View Code.

View Code

Note: you can change the caption and name of a control by right clicking on the control (make sure Design Mode is selected) and then clicking on Properties. Change the caption of the command button to Import Data. For now, we will leave TextBox1 as the name of the text box.

6. Add the following code line:

TextBox1.Text = "Data imported successfully"

7. Click the command button on the sheet (make sure Design Mode is deselected).

Result:

Text Box

8. To clear a text box, use the following code line:

TextBox1.Value = ""

Note: although in some situations it can be useful to directly place a text box on your worksheet, a text box is particularly useful when placed on a Userform.