Search notes:

VBA: function TypeName

typeName(var) returns a string according to the datatype of var.

A simple class

We define a simple class (named tq84) which we will use to determine its type name.
public aMember as double
Github repository about-VBA, path: /functions/typeName/tq84.cls

Calling typeName

option explicit

type tp
    foo as long
    bar as date
end type

sub printTypeName(v as variant)
   debug.print typeName(v)
end sub

sub main()

    dim tp_ as tp
    dim obj as new tq84
    dim var as variant

    printTypeName 42          ' Integer
    printTypeName true        ' Boolean
    printTypeName nothing     ' Nothing
    printTypeName empty       ' Empty
    printTypeName null        ' Null
    printTypeName obj         ' tq84
    printtypeName obj.aMember ' Double

    printTypeName var         ' Empty
    var = #08/28/2010#
    printTypeName var         ' Date

  ' In Excel, it's possible to use typeName to
  ' determine what the application.selection property
  ' actually referrs to. Chances are that it is a range.
    printTypeName selection   ' Range

  ' printTypeName missing     ' error: Variable not defined …

  ' printTypeName tp_  ' Apparently, this is not possible
  '                    ' Error message is: Only user-defined types defined in public object modules
  '                                        can be coerced to or from a variant or passed to late-bound functions

end sub
Github repository about-VBA, path: /functions/typeName/main.bas

See also

typeOf … is
varType()
VBA functions
The (static) TypeName() method of the .NET class Microsoft.VisualBasic.Information.

Index