Adding the values of 2 textboxes

B

Ben Allen

I have textbox1 and textboxchange.
I have code which says:

Cells(1,2).value=textbox1 - textboxchange

This works fine, however this doesnt:
Cells(2,2).value=textbox1 + textboxchange

I simply get the value of textbox1 eg 23 plus the value of textbox change on
the end eg. if txtboxchange=3 the the result is 231.
I'm completly stumped by this. Thanks
--
Cheers,
Ben

Remove your.tonsils to reply
"You only live once but if you do it right once is enough!"
 
H

Hawk

The + is concatenating the two values since the default
data type for a textbox is a string. You need to cast the
textbox value to a number. Try this...

Cells(2, 2).Value = CLng(textbox1) + CLng(textboxchange)
 
M

Melanie Breden

Hi Ben,

Ben said:
I have textbox1 and textboxchange.
I have code which says:

Cells(1,2).value=textbox1 - textboxchange

This works fine, however this doesnt:
Cells(2,2).value=textbox1 + textboxchange

I simply get the value of textbox1 eg 23 plus the value of textbox change on
the end eg. if txtboxchange=3 the the result is 231.
I'm completly stumped by this. Thanks

the values of the textbox are in the string format.
For calculation they must be converted with the
CDbl Function into numeric values.

Cells(1, 2).Value = CDbl(textbox1 ) - CDbl(textboxchange)
Cells(2, 2).Value = CDbl(textbox1 ) + CDbl(textboxchange)

--
Mit freundlichen Grüssen

Melanie Breden
- Microsoft MVP für Excel -

http://excel.codebooks.de (Das Excel-VBA Codebook)
 
H

Hawk

As Melanie said...use CDbl rather than CLng
-----Original Message-----
The + is concatenating the two values since the default
data type for a textbox is a string. You need to cast the
textbox value to a number. Try this...

Cells(2, 2).Value = CLng(textbox1) + CLng(textboxchange)
.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top