UserForms and Variables and Types of data

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 ....)
 
C

Chip Pearson

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
 
R

Ron de Bruin

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
 
L

Larry Levinson

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 ....)
 
L

Larry Levinson

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 ....)
 
J

Jake Marx

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]
 

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