VBA VarType Function

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

The VBA VarType function returns information on the VBA Data Type of a given variable. It returns a number corresponding to one of the values of the VbVarType VBA Enum object.

The Excel VBA VarType function comes useful in a variety of scenarios especially when we are passing Variant objects into a VBA Sub or Function as a parameter.

VarType Function Syntax

The syntax for the VarType function in VBA is:

A VBA variable for which we want to learn the VbVarType.

Example usage

To exemplify the usage of the VBA VarType function let us create a custom VBA Sub called PrintType that will print in the Immediate window the name of the data type we provide:

I listed above all values within the VBA Enum called VbVarType. Now we can use this function to quickly test the data type of a given variable. See examples below:

Simply the best place to learn Excel VBA