Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking whether a string is a number

Status
Not open for further replies.

Chrissirhc

Programmer
May 20, 2000
926
GB
Hi,

If I have a string "70", how do I check whether it is a number?

Thanks,

Chris
 
[tt]IsNumeric()[/tt]?

[small]For the best results do what I'm thinking, not what I'm saying.[/small]
(GMT-07:00) Mountain Time (US & Canada)
 
If yourString Like "*[!0-9]*" Then MsgBox "NOT numeric"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
And if you want is as a number - if it's a number - then you can use ..

Code:
If IsNumeric(myVariable) Then myVariable = Clng(myVariable)

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
IsNumeric is a little quirky, recognising some quite odd strings as numbers. In the US, for example, something such as "1d0$" would be considered by IsNumeric as a number (since it IS a legitimate number in VB)...
 
I didn't get these posts till late so I ended up using a dirty hack:

Application.WorksheetFunction.IsNumber(CDbl(firstCharacter))

and if it throws an error its a letter which I catch later...

If I used:
If yourString Like "*[0-9]*" Then MsgBox "IS numeric"

Would that work? Say I had 5DE, wouldn't that show as numeric?

Thanks in advance,

Chris
 
If Not (yourString Like "*[!0-9]*") Then MsgBox "IS numeric"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
It works, but I don't understand how. I don't understand that syntax.... What do the *[ mean? !0-9 I guess means not 0,1,2,3...9.

Thanks a lot,

Chris
 
Just a quick note on the previous code you posted ..

Code:
Application.WorksheetFunction.IsNumber(CDbl(firstCharacter))

If the variable firstCharacter was not a number, the error would not be raised from the IsNumber function (which you should use the IsNumeric, as it's native and not accessing an object), but rather by the CDbl coercion function.

HTH

Regards,
Zack Barresse

Simplicity is the ultimate sophistication. What is a MS MVP? PODA
- Leonardo da Vinci
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top