adding 2 numbers in textbox - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I created a form which has 3 text boxes and 4 cmd buttons. user will enter number in the first box and another number in the second box and click on +,-,*, / buttons and the result will be in box 3. The -,*, and / work fine but I noticed the + is not.

If user enter 1 in the firstbox and 2 in the secondbox and click on + then the 3rd box will show 12 not 3 ! why is that? and why other operations are ok except +. Thank you very much

Code:
Private Sub plus_Click()
   dim x as integer
   x = first.Value + second.Value
   result.Value = x
End Sub
 
Last edited:

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi lezawang,

The code looks correct :confused: Maybe you've assigned the code that multiplies to the command button for adding?

Note that an Integer variable can only hold numbers in the range of -32,768 to 32,767 so you will receive an error if the total is outside this range.

HTH

Robert
 
Upvote 0
Thanks for your reply. No that is not the case. Even if that the case then the result would have been 2 not 12
first number 1
second number is 2

the result should be 3 (if add)
or 2 if multiplication

but I am getting 12, that seems excel concatenating 1 and 2 that is why I am getting 12. Thanks
 
Upvote 0
Hi, try using this:
x = CInt(first.Value) + CInt(Second.Value)
 
Upvote 0

Forum statistics

Threads
1,224,919
Messages
6,181,763
Members
453,065
Latest member
jfrsanders

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top