Textbox

 

Draw a Textbox    |   Populate a Textbox    |   Clear a Textbox

A textbox is an empty field where the user can fill in a piece of text. Learn how to draw a textbox on your worksheet, how to refer to a textbox in your Excel VBA code, and how to clear a textbox.

Draw a Textbox

Excel 2010 and Excel 2007 users. Click on Insert from the Developer tab and then click on Text Box in the ActiveX Controls section.

Create an Excel VBA Textbox in Excel 2010 or Excel 2007

Excel 2003 users. Click on Text Box from the Control ToolBox.

Create an Excel VBA Textbox in Excel 2003

1. Draw a textbox and a command button on your worksheet.

Draw a Textbox and a Command Button on your Worksheet

2. You can change the caption of the command button by right clicking on the command button and then clicking on Properties and Caption (make sure Design Mode is selected).

3. You can change the name of the textbox by right clicking on the textbox and then clicking on Properties and Name. For now, we will leave TextBox1 as the name of the textbox.

Populate a Textbox

To populate the textbox, execute the following steps:

1. Right click on the command button. Click on View Code. Add the following code line:

TextBox1.Text = "Data imported successfully"


Result when you click the command button on the sheet:

Excel VBA Textbox Result

2. To place text from a textbox into a cell, add a code line like this:

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


Clear a Textbox

To clear a textbox, use the following code line:

TextBox1.Value = ""


Although in some situations it can be useful to directly place a textbox on your worksheet, a textbox is particularly useful when placed on a Userform.

Did you find this information helpful? Show your appreciation, vote for us.

Go to Top: Textbox    |   Go to Next Topic: Listbox

Learn more about textboxes, Login to the right >>
100 easy to follow Excel VBA examples. Limited time-offer: $39.00 but only $29.00. Ends on 31 May.