IsNumber (in Rick Rothstein's example)

S

Sam Kuo

I'm trying to write a function that checks whether an input is a positive
number.

Below is one of Rick Rothstein's examples that he recommands for use in
replacement of IsNumeric. But I need to expand it further so only "positive
whole number or integer" (i.e with or without decimal point) returns true.
While anything else with text, sign or symbol need to return false.

Any help is appreciated.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function
 
R

Rick Rothstein \(MVP - VB\)

Rightfully, the function you posted should have been called IsDigits, not
IsNumber.

Give this function a try...

Function IsNumber(ByVal Value As String) As Boolean
' Uncomment the next statement out if you
' want to provide for plus/minus signs
' If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick
 
T

Tom Hutchins

Try...

Function IsNumber(ByVal Value As String) As Boolean
If (Len(Value) > 0 And Not Value Like "*[!0-9.]*") Then
If Value > 0 Then
IsNumber = True
Exit Function
End If
End If
IsNumber = False
End Function


Hope this helps,

Hutch
 
R

Rick Rothstein \(MVP - VB\)

I just re-read what you wrote and I'm not sure what your question really is
anymore. You wrote "so only 'positive whole number or integer' (i.e with or
without decimal point) returns true"... and so, picking up on the "decimal
point" part (and reading over the rest of what you wrote kind of quickly), I
gave you a function for seeing if an entry is a floating point number or
not. But I now think maybe you are asking about permitting a trailing
decimal point. If that is actually what you want, try this function
instead...

Function IsNumber(ByVal Value As String) As Boolean
If Value Like "." Then Value = Left(Value, Len(Value) - 1)
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function

Rick


Rick Rothstein (MVP - VB) said:
Rightfully, the function you posted should have been called IsDigits, not
IsNumber.

Give this function a try...

Function IsNumber(ByVal Value As String) As Boolean
' Uncomment the next statement out if you
' want to provide for plus/minus signs
' If Value Like "[+-]*" Then Value = Mid$(Value, 2)
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Rick


Sam Kuo said:
I'm trying to write a function that checks whether an input is a positive
number.

Below is one of Rick Rothstein's examples that he recommands for use in
replacement of IsNumeric. But I need to expand it further so only
"positive
whole number or integer" (i.e with or without decimal point) returns
true.
While anything else with text, sign or symbol need to return false.

Any help is appreciated.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function
 
R

Rick Rothstein \(MVP - VB\)

Below is one of Rick Rothstein's examples that he recommands
for use in replacement of IsNumeric.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function

Rightfully, the function you posted should have been called IsDigits, not
IsNumber.

I just noticed... you added a decimal point inside the square-brackets that
I had not included in my original posting. Doing that invalidates the
function... for that modification you did, things like the following will
now pass the test...

Debug.Print IsNumber(".")
Debug.Print IsNumber("...")
Debug.Print IsNumber("1.2.3")

See my other posts in this thread for the function you want.

Rick
 
S

Sam Kuo

Hi Rick

Thanks for your explaination.
Sorry you were right that I was really after IsNumber, not IsDigits of your
example.

I want to only allow positive whole number (without decimal point) and real
number (with decimal point) to return TRUE. For example, the acceptable input
might be 0.25 or 100 say.

Your IsNumber here works great for this purpose. But I also need input with
signs (e.g. $123) or exponential (1E2) to return FALSE. Is this doable?

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Many thanks
Sam




Rick Rothstein (MVP - VB) said:
Below is one of Rick Rothstein's examples that he recommands
for use in replacement of IsNumeric.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function

Rightfully, the function you posted should have been called IsDigits, not
IsNumber.

I just noticed... you added a decimal point inside the square-brackets that
I had not included in my original posting. Doing that invalidates the
function... for that modification you did, things like the following will
now pass the test...

Debug.Print IsNumber(".")
Debug.Print IsNumber("...")
Debug.Print IsNumber("1.2.3")

See my other posts in this thread for the function you want.

Rick
 
R

Rick Rothstein \(MVP - VB\)

You are going to explain in some detail exactly how you are using this
function. The reason is because the function already returns False for
numbers like $123 and 1E2... that is what it was designed to do.

Rick


Sam Kuo said:
Hi Rick

Thanks for your explaination.
Sorry you were right that I was really after IsNumber, not IsDigits of
your
example.

I want to only allow positive whole number (without decimal point) and
real
number (with decimal point) to return TRUE. For example, the acceptable
input
might be 0.25 or 100 say.

Your IsNumber here works great for this purpose. But I also need input
with
signs (e.g. $123) or exponential (1E2) to return FALSE. Is this doable?

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Many thanks
Sam




Rick Rothstein (MVP - VB) said:
Below is one of Rick Rothstein's examples that he recommands
for use in replacement of IsNumeric.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function

Rightfully, the function you posted should have been called IsDigits,
not
IsNumber.

I just noticed... you added a decimal point inside the square-brackets
that
I had not included in my original posting. Doing that invalidates the
function... for that modification you did, things like the following will
now pass the test...

Debug.Print IsNumber(".")
Debug.Print IsNumber("...")
Debug.Print IsNumber("1.2.3")

See my other posts in this thread for the function you want.

Rick
 
S

Sam Kuo

Thanks Rick and Tom.

Rick, it's working fine now :)
Thanks again for your input.

Sam


Rick Rothstein (MVP - VB) said:
You are going to explain in some detail exactly how you are using this
function. The reason is because the function already returns False for
numbers like $123 and 1E2... that is what it was designed to do.

Rick


Sam Kuo said:
Hi Rick

Thanks for your explaination.
Sorry you were right that I was really after IsNumber, not IsDigits of
your
example.

I want to only allow positive whole number (without decimal point) and
real
number (with decimal point) to return TRUE. For example, the acceptable
input
might be 0.25 or 100 say.

Your IsNumber here works great for this purpose. But I also need input
with
signs (e.g. $123) or exponential (1E2) to return FALSE. Is this doable?

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Not Value Like "*[!0-9.]*" And _
Not Value Like "*.*.*" And _
Len(Value) > 0 And Value <> "." And _
Value <> vbNullString
End Function

Many thanks
Sam




Rick Rothstein (MVP - VB) said:
Below is one of Rick Rothstein's examples that he recommands
for use in replacement of IsNumeric.

Function IsNumber(ByVal Value As String) As Boolean
IsNumber = Len(Value) > 0 And Not Value Like "*[!0-9.]*"
End Function

Rightfully, the function you posted should have been called IsDigits,
not
IsNumber.

I just noticed... you added a decimal point inside the square-brackets
that
I had not included in my original posting. Doing that invalidates the
function... for that modification you did, things like the following will
now pass the test...

Debug.Print IsNumber(".")
Debug.Print IsNumber("...")
Debug.Print IsNumber("1.2.3")

See my other posts in this thread for the function you want.

Rick
 

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