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 data type 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()".
Function Returned Value
"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 between the following:
An object variable containing an Excel.Range
and an Excel.Range object
Examples
' Declare variables.
Dim sNullVar As String
Dim sMyType As String
Dim sStringVar As String
Dim iIntegerVar As Integer
Dim cCurrencyVar As Currency
Dim arArrayVar (1 To 5) As Integer
sNullVar = Null ' Assign Null value.
sMyType = TypeName(sStringVar) ' Returns "String".
sMyType = TypeName(iIntegerVar) ' Returns "Integer".
sMyType = TypeName(cCurrencyVar) ' Returns "Currency".
sMyType = TypeName(sNullVar) ' Returns "Null".
sMyType = TypeName(arArrayVar) ' Returns "Integer()".
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext