How to Add Text to Textbox Using Excel VBA (3 Effective Ways)

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.

inserting textbox to add text to a Textbox using Excel VBA

  • 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.

opening visual basic to add text to a Textbox using Excel VBA

  • In the new window, click on Insert and select Module.

inserting module to add text to a Textbox using Excel VBA

  • Enter the following code:
Public Sub TypingValue()
ActiveSheet.Shapes("TextBox 2").TextFrame.Characters.Text = "California"
End Sub

macro code to add text to a Textbox using Excel VBA

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.

opening macros to add text to a Textbox using Excel VBA

  • 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

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Nazmul Hossain Shovon
Nazmul Hossain Shovon

Nazmul Hossain Shovon, a BUET graduate in Naval Architecture and Marine Engineering, embarked on his career with 8 months dedicated to the Exceldemy project's triumph. Transitioning into a Software Developer role, he specialized in web add-in development. At Exceldemy, he authored about 125 blog articles and solved many visitors’ problems, refining his writing skills and delving into Excel-related topics. With a primary passion for programming and software development, Shovon continually explores new horizons, fostering professional growth in his... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo