UserForms and Variables and Types of data

  • Thread starter Thread starter Larry Levinson
  • Start date Start date
L

Larry Levinson

I have a user form with four textboxes for the user to input numbers
.... How do I tell the macro that it shoud treat the input as a number
and not as text? this is not about validation. Let's assume, just for
the sake of arguments that the user is smart enough to put in numbers
to begin with ...

thanks ...


Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 
Larry,

Text boxes always contain text (which may be all numbers), not
actual numeric data. Thus, you need to convert the text to a
number using a function like CInt, CLng, or CDbl. For example,

Dim N As Long
With Me.TextBox1
If IsNumeric(.Text) = True Then
N = CLng(.Text)
Else
' not numeric text in text box
End If
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Hi Larry

You must convert the string to a value

MsgBox Val(TextBox1.Value) + Val(TextBox2.Value)
Or
MsgBox CDbl(TextBox1.Value) + CDbl(TextBox2.Value)

If you need some code to validate post back
 
so, assuming the user is putting in a number anyway, it would be


BN = CLng(DataForm.BN.Value)
var textbox ...

right?

Chip Pearson said:
Larry,

Text boxes always contain text (which may be all numbers), not
actual numeric data. Thus, you need to convert the text to a
number using a function like CInt, CLng, or CDbl. For example,

Dim N As Long
With Me.TextBox1
If IsNumeric(.Text) = True Then
N = CLng(.Text)
Else
' not numeric text in text box
End If
End With

Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 
grazie .. now, about LONG versus SINGLE data types. one of my
variables gets to be 1,000+ with a decimal. shoud it be single instead
of long?




Chip Pearson said:

Larry Levinson
Talking up to the vocal ...
LLevinson*Bloomberg.net
(remove the star etc ....)
 
Hi Larry,

Larry said:
grazie .. now, about LONG versus SINGLE data types. one of my
variables gets to be 1,000+ with a decimal. shoud it be single instead
of long?

Yes. A Long can hold only a whole number, so if you have a decimal part of
the number, you'll need to use Single or Double (depending on what type of
accuracy you want).

--
Regards,

Jake Marx
MS MVP - Excel
www.longhead.com

[please keep replies in the newsgroup - email address unmonitored]
 
Back
Top