6

I loop over string variable data which may have integer numeric value, like "123". If this string variable has numeric value, I want to indicate it and thought to use some like this:

If IsNumeric(CInt(data)) Then 
    WScript.Echo "Number"
Else
    WScript.Echo "String"
End If

But CInt() raises error every time data variable can't be converted to integer:

Type mismatch: 'CInt'

How can I indicate if string has integer value in vbscript?

1
  • Just found out that CInt() is not necessary for IsNumeric() to function as expected. Removing CInt() function is solution. Commented Jul 12, 2012 at 11:08

2 Answers 2

11

IsNumeric Function of vb script can be used to determine whether an expression can be evaluated as a number.It returns Boolean value depending upon expression

Please be noted that IsNumeric returns False if expression is a date expression.

Now, in your code you have mistaken that even if it is not number, you trying to convert it into integer

You can use this in your code like this--

If IsNumeric(data) Then       
   WScript.Echo "Number"
Else
   WScript.Echo "String"
End If
Sign up to request clarification or add additional context in comments.

1 Comment

I am sure this is a common knowledge but if the string you pass into the IsNumeric() looks like this "1013230924d3" or this "1013230924E3" it will still evaluate to TRUE. D and E are considered scientific notations and things tend to slip through the crack :)
1

only for integers:

If VarType(data) = vbInteger Then
    WScript.Echo "Integer"
Else
    WScript.Echo "Something else"
End If

for numbers:

If IsNumeric(data)
    WScript.Echo "Number"
Else
    WScript.Echo "string"
End If

1 Comment

For VBScript, which the OP's question is tagged with at least now, one needs to swap vbInteger for the number in (for example) this page which corresponds to the variable they are looking to check type of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.