My recommendation
Use VarType
for built-in types covered by an enumeration VbVarType
. Use TypeName
for other types. I will explain this recommendation in detail below.
Performance
The performance difference is likely to be negligible, especially if you use VBA to write database applications.
Vartype
VarType
, : vbDouble
, (, Option Explicit
, ). "Double()"
, .
TypeName
TypeName
, , VbVarType
:
Dim b As New Collection
Dim a As Variant
Set a = b
Debug.Print VarType(a) ' Prints just the generic vbObject constant
Debug.Print TypeName(a) ' Prints "Collection"
Gotchas
, , VarType
, , vbObject
. MS Access VBA TempVar:
TempVars("x") = 123
Dim a As Variant
Set a = TempVars("x")
Debug.Print VarType(a) ' Prints vbInteger, the type of a.Value current content.
' (Value is TempVar default property)
Debug.Print TypeName(a) ' Prints "TempVar"