How to Add a Textbox in Excel
STEPS:
- Go to the Developer tab and select the Insert icon. A drop-down menu will appear.
- Select the Text Box icon from the ActiveX Controls section.
- Put the cursor on the sheet. It will turn into a small black plus sign.
- Draw a textbox on the sheet by left-clicking and dragging, then releasing the mouse button.
Read More: How to Hide Textbox Using Excel VBA
Excel VBA to Convert a Textbox Value to a Number: 2 Examples
Method 1 – Excel VBA to Convert Textbox Values to Numbers Using Cell
We will store a decimal number as a text value in a cell. The textbox will convert that decimal number into an integer number and store it. In the picture below, you can see Cell B5 contains 22.75 as a text value and a Textbox right beside it.
STEPS:
- Go to the Developer tab and select Design Mode.
- Right-click on the textbox to open the Context Menu.
- Select View Code.
You will see a Code window like the picture below.
- In the Code window, insert the following code:
Private Sub TextBox1_Change()
TextBox1.Value = CInt(Range("B5"))
End Sub
We have inserted a single line of code inside default commands. This code will convert the text value from B5 into an integer number. If you want to convert the value into double or long format, then type CDbl or CLng in place of CInt.
- Press Ctrl + S to save the code.
- Close the Visual Basic window.
- Go to the Developer tab and deselect Design Mode.
- Click inside the Textbox and press any key to see the result.
- Change the value of Cell B5 to 33.33.
- Put the cursor inside the Textbox.
- Press any key to see the updated result.
Read More: How to Convert TextBox Value to Cell with VBA in Excel
Method 2 – Add a Textbox to Convert a Value to a Number with Excel VBA
We will enter two values in two textboxes and show their sum in the third textbox. Here are the textboxes we added.
STEPS:
- Go to the Developer tab and select Design Mode.
- Right-click on the first textbox.
- Select View Code from the Context menu. This will open the Code window.
- In the Code window, you will see the default commands for the 3 text boxes.
- Paste the code below inside each section of the text boxes like the picture below:
If IsNumeric(TextBox1) And IsNumeric(TextBox2) Then
TextBox3 = CStr(CLng(TextBox1) + CLng(TextBox2))
End If
This code will check if TextBox1 and TextBox2 contain numbers. If they contain numbers, then it will add them and store them in TextBox3. You must paste the same code for each textbox. The names of the textboxes must align.
- Press Ctrl + S to save the code.
- Close the Visual Basic window.
- Navigate to the Developer tab and deselect Design Mode.
- Type 23 in Textbox 1 and 12 in Textbox 2.
- Click inside Textbox 3 and press any key to see the result.
- You can change the values of Textbox 1 and 2 to see the change in Textbox 3.
Read More: How to Format TextBox for Phone Number in Excel VBA
Download the Practice Workbook
Related Articles
- How to Make Textbox Datepicker with Excel VBA
- How to Use VBA to Change Textbox Text in Excel
- How to Add Text to Textbox Using Excel VBA
- How to Use Excel VBA Textbox Events
- How to Use VBA Textbox Properties in Excel