TypeName Function

This function returns a string containing the name of the data type that is stored in a variable.


Be aware that the current selection is not always what you might expect.
It is worth using the TypeName() function on the selection object to determine what is currently selected
This function can be used on the selection object to determine what is currently selected.
If you use the Variant datatype for your variables VBA uses automatic data type conversion to ensure that the data type is always large enough to contain the necessary data.
The TypeName function returns the class name of an object
This returns the subtype of a Variant and therefore cannot distinguish between the following:
An object variable containing an Excel.Range and an Excel.Range object


The TypeName function returns a string that describes what is currently selected
TypeName(varname)


The required varname argument is of type Variant and can contain any variable except a user defined type


If varname is an array, the returned string can be any one of the possible returned strings (or Variant) with empty parentheses appended. For example, if varname is an array of integers, TypeName returns "Integer()".



Returned String


"Array()"Array
"Boolean"Boolean value
"Byte"Byte value
"Currency"Currency value
"Date"Date value
"Decimal"Decimal value
"Double"Double-precision floating-point number
"Empty"Uninitialized
"Error"An error value
"Integer"Integer
"Long"Long integer
"Nothing"Object variable that doesn't refer to an object
"Null"No valid data
"Object Type"An object whose type is ObjectType, for example Range, Sheet, Shape etc
"Object"An object
"Single"Single-precision floating-point number
"String"String
"Unknown"An object whose type is unknown

Identifying an Array

If you pass in an array variable the string returned will be the name of the data type with a "()" at the end.

Dim myArray() As Long 
Call MsgBox(TypeName(myArray)) = "Long()"

Sub Types

This function cannot distinguish betwee the following:
An object variable containing an Excel.Range
and an Excel.Range object


Examples

' Declare variables.
Dim NullVar As String
Dim MyType As String
Dim StrVar As String
Dim IntVar As Integer
Dim CurVar As Currency
Dim ArrayVar (1 To 5) As Integer

NullVar = Null ' Assign Null value.
MyType = TypeName(StrVar) ' Returns "String".
MyType = TypeName(IntVar) ' Returns "Integer".
MyType = TypeName(CurVar) ' Returns "Currency".
MyType = TypeName(NullVar) ' Returns "Null".
MyType = TypeName(ArrayVar) ' Returns "Integer()".


© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext