To check if a variant variable is a range, you can use the VBA function TypeName. Here's an example:
Formula:
Sub CheckIfRange()
Dim myVar As Variant
Set myVar = Range("A1:B2")
If TypeName(myVar) = "Range" Then
MsgBox "The variable is a range."
Else
MsgBox "The variable is not a range."
End If
End Sub
In this example, we create a variant variable called
myVar and set it equal to a range of cells. We then use the
TypeName function to check if
myVar is a range. If it is, we display a message box saying so. If it's not, we display a message box saying that it's not a range.
- Create a variant variable called myVar.
- Set myVar equal to a range of cells using the Range function.
- Use the TypeName function to check if myVar is a range.
- Display a message box indicating whether or not myVar is a range.
Note that the
TypeName function will return the data type of any variable, not just ranges. So if you want to check for other data types, you can modify the code accordingly.