PDA

View Full Version : textbox to integer



jhnnyboz
06-18-2012, 09:35 AM
hey i am trying to calculate the water/ cementious ratio using a user form and for some reason vba is saving the textbox entry to a text so then why i try to add three inputs together say for 1,1,1 it gives me 111 instead of 3. but i have another input that it will subtract and divide just fine.

please help



Dim course, fine, cement, water1 As Integer

'calculate
course = w1 - w1 / (1 + m1 / 100)
fine = w2 - w2 / (1 + m2 / 100)
cement = course + fine + cement1 + pozz
water1 = 8.33 * (addedwater + batchwater + meterwater)
cwratio1 = water1 / cement
MsgBox course
MsgBox fine
MsgBox water1

it will calculate the course and fine as integers but the water1 it will give me the "111" instead of 3.

CatDaddy
06-18-2012, 09:39 AM
you can try CInt on your variables in case one is a string

CatDaddy
06-18-2012, 09:40 AM
+ is also a concatenation operator on strings

jhnnyboz
06-18-2012, 10:23 AM
oh that makes sense.

can you post the code for cint, i am not familiar with that.

CatDaddy
06-18-2012, 10:35 AM
i assume you have something like
w1 = textbox(blahblahblah)

change it to
w1 = CInt(textbox(blahblahblah))

CodeNinja
06-18-2012, 11:11 AM
Jhnnyboz,
Just a note, I see you are using integers, but you may want to consider using doubles instead of integers....

With an integer, any fractions are rounded. If you want this that is fine, but some of your calculations look like they need decimal values. If you were to change all or some of the variables to doubles, it may give you more accurate calculations.

Of course you could use cdbl() the same way as you use cint() to convert a string or value to a double.

Good luck.

CatDaddy
06-18-2012, 11:17 AM
^^true, i did not even consider that