How to check a Variant for its data type

T

TBA

Windows 2k Pro
Excel 2000

I have a 1-dimensional array as Variant. The values are either a String or
an Integer. How do I loop through the array and check to see if a
particular value is a string or an integer?

Thanks!

-gk-
 
R

Ripan

As an add-on question, what if you want to check if a string is a
integer or not? Do you need to manually check each character?

Thank
 
C

Chip Pearson

You can use the TypeName function to return the type of value
contained in the variant. For example,

Dim V As Variant
V = 123
Debug.Print TypeName(V)

You can determine whether a string is numeric by using the
IsNumeric function. For example,

Dim V As Variant
V = "1234"
Debug.Print IsNumeric(V)


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

Ripan

Fair enough. IsNumeric makes the distinction between numeric and not
but what if I only want integers, and not floats
 
C

Chip Pearson

Try something like

Dim V As Variant
V = whatever
If IsNumeric(V) Then
If CInt(V) = CDbl(V) Then
Debug.Print "is integer"
Else
Debug.Print "not an integer"
End If
End If



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