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 initializedvbNull (1)– Null (no valid data)vbInteger (2)– 16-bit integervbLong (3)– 32-bit integervbSingle (4)– single-precision floatvbDouble (5)– double-precision floatvbCurrency (6)– currencyvbDate (7)– date/timevbString (8)– stringvbBoolean (11)– True/FalsevbVariant (12)– Variant (rarely returned directly)vbObject (9)– object referencevbError (10)– error valuevbArray (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:
VarTypereturns a numeric constant;TypeNamereturns a string.TypeNamefor an object returns the class name (e.g., “Range”), whileVarTypejust returnsvbObject.TypeNamereturns “Empty” for uninitialized Variants;VarTypereturns 0.- Comparing with
VarTypeis 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
Variantonly when the type is truly unknown (e.g., reading from a cell). - Combine
VarTypewithIsArrayandIsObjectfor complete type checking. - Remember that
VarTypeon a Variant containing an Error value returnsvbError(10). You can create one withCVErr(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, andvbEmpty(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
- What Is Enum in JavaScript? Real Usage
- Shortcuts on Laptop: The Complete Guide
- How to Update a Dictionary in Appian (AEP)
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel VBA Date String Formatting ExplainedIn Excel VBA, use Format$ to turn a date value into a formatted text string, such as Format$(Date,…
- Excel VBA Compile Error in Hidden Module: How to FixAn Excel VBA compile error in a hidden module usually means that code in a workbook, add-in, or…
- Access VBA MsgBox: Syntax, Buttons, and ExamplesIn Microsoft Access VBA, use MsgBox to display a message or ask a user a simple question; its…
- Excel VBA: Add a New Sheet at the End and Name It SafelyTo add a worksheet after the workbook’s last tab and name it in Excel VBA, use Worksheets.Add with…
- How to Find VBA Code in Excel: Open and Search MacrosTo find VBA code in Excel for Microsoft 365 on Windows, open the workbook in the desktop app,…
- Excel VBA Dictionary of Arrays: How to Store and Use DataYes—you can store arrays as values in an Excel VBA Dictionary. On Windows, create a Scripting.Dictionary, assign an…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.