Method 1 – Typing Value in VBA to Add Text to Textbox in Excel
- Go to the Insert tab and click on the Text Box under Text.
- Click on the cell where you want to place the Textbox.
- This will result in an empty Textbox.
- Go to the Developer tab and click on Visual Basic.
- In the new window, click on Insert and select Module.
- Enter the following code:
Public Sub TypingValue()
ActiveSheet.Shapes("TextBox 2").TextFrame.Characters.Text = "California"
End Sub
In the above code, ActiveSheet is referencing the worksheet that we are currently in. Shapes(“TextBox 2”) selects the Textbox to work with. TextFrame allows us to manipulate the elements of the Textbox. The Characters command allows us to format characters in a text string.
- Go to the Developer and click on Macros.
- In the new Macro window, select the macro TypingValue and click on Run.
- This will add the text California inside the Textbox.
- You can add any other value inside other text boxes.
Read More: How to Convert TextBox Value to Cell with VBA in Excel
Method 2 – Using Range Property in VBA
- Insert a Textbox within your dataset.
- Open the VBA module window and insert the following code:
Public Sub RangeProperty()
ActiveSheet.Shapes("TextBox 1").TextFrame.Characters.Text = Range("D5").Value
End Sub
The code above functions the same as the code in Method 1, except for the Range(“D5”).Value portion which takes the range of cells that hold the texts that we want to add to the Textbox.
- Open the macro window and run the VBA.
- This will add the appropriate text inside the empty Textbox.
- Follow the same procedure to add as many Textboxes as you want inside the worksheet.
Read More: How to Use VBA Textbox Properties in Excel
Method 3 – Applying VBA If Statement
- Create a new Textbox inside the data table.
- Open a VBA module and enter the following code.
Sub VBAIfStatement()
Sheet_Name = "VBAIF"
Shape_Name = "TextBox 2"
To_be_Replaced = " "
Replaced_with = "California"
If InStr(1, Sheets(Sheet_Name).Shapes(Shape_Name).TextFrame.Characters.Text, To_be_Replaced) <> 0 Then
Sheets(Sheet_Name).Shapes(Shape_Name).TextFrame.Characters.Text = Replace(Sheets(Sheet_Name).Shapes(Shape_Name).TextFrame.Characters.Text, To_be_Replaced, Replaced_with)
End If
End Sub
In the above code, the If-Then statement checks whether a given condition is true or not. Based on this it will perform certain actions. The InStr method determines the first occurrence of one string within another string. The End If command terminates the If statement operation.
- Select the VBAIfStatement macro and click on Run.
- The VBA code will add the new text inside the empty Textbox as shown in the image below.
- Repeat the steps for all of the remaining text boxes where you want to add any new text.
Download Practice Workbook
Related Articles
- How to Make Textbox Datepicker with Excel VBA
- How to Hide Textbox Using Excel VBA
- How to Use VBA to Change Textbox Text in Excel
- 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