Excel VBA to Convert Textbox Value to Number (2 Ideal Examples)

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.

How to Add Textbox in Excel?

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

Excel VBA to Convert Textbox Value to Number Using Cell

STEPS:

  • Go to the Developer tab and select Design Mode.

Excel VBA to Convert Textbox Value to Number Using Cell

  • Right-click on the textbox to open the Context Menu.
  • Select View Code.

Excel VBA to Convert Textbox Value to Number Using Cell

You will see a Code window like the picture below.

Excel VBA to Convert Textbox Value to Number Using Cell

  • In the Code window, insert the following code:
Private Sub TextBox1_Change()
TextBox1.Value = CInt(Range("B5"))
End Sub

Excel VBA to Convert Textbox Value to Number Using Cell

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.

Excel VBA to Convert Textbox Value to Number Using Cell

  • Click inside the Textbox and press any key to see the result.

Excel VBA to Convert Textbox Value to Number Using Cell

  • Change the value of Cell B5 to 33.33.
  • Put the cursor inside the Textbox.

Excel VBA to Convert Textbox Value to Number Using Cell

  • Press any key to see the updated result.

Excel VBA to Convert Textbox Value to Number Using Cell

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.

Add Textbox to Convert Value to Number with Excel VBA

STEPS:

  • Go to the Developer tab and select Design Mode.

Add Textbox to Convert Value to Number with Excel VBA

  • 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

Get FREE Advanced Excel Exercises with Solutions!

Tags:

Mursalin Ibne Salehin
Mursalin Ibne Salehin

Mursalin Ibne Salehin holds a BSc in Electrical and Electronics Engineering from Bangladesh University of Engineering and Technology. Over the past 2 years, he has actively contributed to the ExcelDemy project, where he authored over 150 articles. He has also led a team with content development works. Currently, he is working as a Reviewer in the ExcelDemy Project. He likes using and learning about Microsoft Office, especially Excel. He is interested in data analysis with Excel, machine learning,... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo