VBA VarType: What It Is and How to Use It

VBA & macros · Updated November 23, 2026

In VBA, “var type” almost always refers to VarType, a built-in function that returns a number identifying the data type of a variable or expression. For example, VarType(MyVar) returns 2 for an Integer, 8 for a String, and 0 for an empty Variant. This article explains how VarType works, why you’d use it, and how it differs from declaring types with Dim.

What VarType Actually Returns

VarType returns an Integer constant that maps to a VBA data type. The most common values are:

  • vbEmpty (0) – Variant that hasn’t been initialized
  • vbNull (1) – Null (no valid data)
  • vbInteger (2) – 16-bit integer
  • vbLong (3) – 32-bit integer
  • vbSingle (4) – single-precision float
  • vbDouble (5) – double-precision float
  • vbCurrency (6) – currency
  • vbDate (7) – date/time
  • vbString (8) – string
  • vbBoolean (11) – True/False
  • vbVariant (12) – Variant (rarely returned directly)
  • vbObject (9) – object reference
  • vbError (10) – error value
  • vbArray (8192) – array (added to the base type)

When an array is passed, VarType returns the array flag (8192) plus the element type. So an array of Longs returns 8195 (8192 + 3).

How to Use VarType in Code

The syntax is simple: VarType(varname) where varname is any expression or variable. Here’s a practical example:

Dim x As Variant
x = "Hello"
Debug.Print VarType(x)  ' Returns 8 (vbString)
x = 42
Debug.Print VarType(x)  ' Returns 2 (vbInteger)
x = #1/1/2025#
Debug.Print VarType(x)  ' Returns 7 (vbDate)

You can also use it in a Select Case to branch on type:

Sub ShowType(v As Variant)
    Select Case VarType(v)
        Case vbInteger, vbLong
            MsgBox "It's a number"
        Case vbString
            MsgBox "It's text"
        Case vbBoolean
            MsgBox "It's a boolean"
        Case Else
            MsgBox "Something else"
    End Select
End Sub

Why Use VarType Instead of TypeName?

VBA also has TypeName, which returns a string like “Integer” or “String” instead of a number. The main differences:

  • VarType returns a numeric constant; TypeName returns a string.
  • TypeName for an object returns the class name (e.g., “Range”), while VarType just returns vbObject.
  • TypeName returns “Empty” for uninitialized Variants; VarType returns 0.
  • Comparing with VarType is faster in tight loops because integer comparison is cheaper than string comparison.

In practice, use TypeName when you need the specific object type, and VarType when you only care about the primitive category.

Common Pitfalls and Misconceptions

One frequent mistake is assuming VarType reveals the declared type of a variable. It doesn’t – it reveals the type of the current value. For example:

Dim i As Integer
i = 32767
Debug.Print VarType(i)  ' Returns 2 (vbInteger)
i = 40000  ' Overflow error!

But if you declare Dim v As Variant and assign v = 40000, VarType(v) returns 2 (Integer) because 40000 fits in an Integer? No – 40000 exceeds Integer’s max of 32767, so VBA stores it as Long (returns 3). The key point: VarType reflects the actual storage, not your declaration.

Another pitfall: VarType on an object returns vbObject (9) regardless of what object it is. To get the object’s class, use TypeName.

Also note that VarType on an uninitialized object variable returns vbObject (9) only if the variable is declared As Object; if it’s a Variant holding Nothing, it returns vbObject? Actually, VarType(Nothing) returns 9, but VarType(Empty) returns 0. Be careful with Is Nothing checks.

Using VarType to Validate Input

A common real-world use is checking what a user entered in a cell or an input box. For example, to ensure a cell contains a number before doing math:

Sub CheckCell()
    Dim val As Variant
    val = Range("A1").Value
    If VarType(val) = vbEmpty Then
        MsgBox "Cell is empty"
    ElseIf VarType(val) = vbString Then
        MsgBox "Cell has text: " & val
    ElseIf VarType(val) = vbDouble Or VarType(val) = vbInteger Or VarType(val) = vbLong Then
        MsgBox "Cell has number: " & val
    Else
        MsgBox "Cell has something else"
    End If
End Sub

This is more reliable than IsNumeric because IsNumeric("123") returns True (it’s a numeric string), but VarType tells you it’s actually text.

VarType and Arrays

When you pass an array to VarType, you get the array flag (8192) added to the element type. To check if something is an array, use IsArray first, then VarType for the element type. For example:

Dim arr() As Long
ReDim arr(1 To 3)
Debug.Print VarType(arr)  ' Returns 8195 (8192 + 3)
Debug.Print IsArray(arr)  ' Returns True

If you only care whether something is an array, use IsArray – it’s clearer. Use VarType only when you need the element type as well.

Frequently Asked Questions

Does VarType work on objects like Range? Yes, it returns vbObject (9) for any object reference, but it won’t tell you it’s a Range. Use TypeName(Range("A1")) to get “Range”.

What’s the difference between VarType and TypeName for a date? VarType(#1/1/2025#) returns 7 (vbDate), while TypeName(#1/1/2025#) returns “Date”. They’re equivalent for dates.

Can VarType return a value for an undeclared variable? If you use Option Explicit and don’t declare, you’ll get a compile error. Without it, an undeclared variable is a Variant, and if never assigned, VarType returns 0 (vbEmpty).

Why does VarType return 8 for a string that looks like a number? Because the variable holds a string, not a number. VBA doesn’t auto-convert for VarType; it reports the actual storage type. If you need to know if the string can be converted, use IsNumeric.

Is there a way to get the declared type of a variable, not the value type? No, VBA doesn’t expose declared types at runtime. VarType and TypeName only reflect the current value’s type. For a variable declared As Integer, if you assign a string, you’ll get a type mismatch error at runtime, so the declared type is enforced.

Practical Tips for Using VarType

  • Always declare variables with explicit types when possible – it’s faster and avoids surprises. Use Variant only when the type is truly unknown (e.g., reading from a cell).
  • Combine VarType with IsArray and IsObject for complete type checking.
  • Remember that VarType on a Variant containing an Error value returns vbError (10). You can create one with CVErr(2023).
  • For debugging, use the Immediate window: ? VarType(Range("A1").Value) prints the number.
  • In Excel, cell values often come as vbDouble (5) for numbers, vbString (8) for text, vbDate (7) for dates, and vbEmpty (0) for blank cells.

Understanding VarType helps you write robust code that handles unexpected input gracefully. It’s a small function with a big impact on error handling and data validation.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.