VarType returning "wrong" result for Variant data !

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,596
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

Does anybody know why VarType returns vbString when it should return vbObject ?
VBA Code:
Sub Test()

    Dim v As Variant
  
    Set v = Application
    MsgBox VarType(v)  '<== returns 8(vbString) instead of 9(vbObject) !!

End Sub

when assigning other objects to the variant variable other than "Application" , it seems to work as expected.

I have also tried Set v = Excel.Application and Set v = Application.Application but still no joy.

TypeName does work fine and returns Application as expected.

Am I missing something ?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
If you type this into the Immediate Window...

? Application

It returns this...

Microsoft Excel

which looks like a text string to me (it is also what is returned if you append .Name onto the end of Application). I'm guessing that Application defaults to its Name property when not being used in a situation that needs it to be an object.

EDIT NOTE: I just saw this note in the help files for Application... "Use the Application property to return the Application object".
 
Last edited:
Upvote 0
If you type this into the Immediate Window...

? Application

It returns this...

Microsoft Excel

which looks like a text string to me (it is also what is returned if you append .Name onto the end of Application). I'm guessing that Application defaults to its Name property when not being used in a situation that needs it to be an object.
Thanks Rick,

When using Set Keyword it should resolve to the application object ... Also, that's why I tested Application.Application to ensure it doesn't default to the Name Property but still won't work.
 
Upvote 0
In fact it doesn't work , even when assigning the Application object to an Object or Application Variable !
VBA Code:
Sub Test()

    Dim v As Application
    
    Set v = Application
    MsgBox VarType(v)  '<== returns 8(vbString) instead of 9(vbObject) !!

End Sub
 
Upvote 0
The native VarType is problematic. Here is a replacement that works because it looks at the actual data type which is stored in the first 2 bytes of the variant.

VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As LongPtr)
#Else
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
#End If

Private Function MyVarType(ByRef v As Variant) As VbVarType
    Call CopyMemory(MyVarType, v, 2)
End Function

Sub Test()
    Dim v As Variant
    Set v = Application
    Debug.Print VarType(v), MyVarType(v)
End Sub

Excerpt from Documentation reagarding the Variant Data Type:
A variable of variant type, for brevity called a "variant", as defined in Visual Basic, needs 16 bytes storage and its layout is as follows:
Offset || Size || Description
0 || 2 || The value returned by VarType; specifies what kind of data the variant contains.
2 || 6 || Reserved bytes; should be set to zero.
8 || up to 8 The data the variant contains.
 
Upvote 0
Hi Jaafar,

Hm, hever tried to test this...
At least Debug.Print IsObject(Application) works
 
Upvote 0
Hi Jaafar,

Hm, hever tried to test this...
At least Debug.Print IsObject(Application) works
Thanks ZVI,

IsObject works but I just wanted to know why VarType doesn't return vbObject in this case as one would expect.
 
Upvote 0
It is because the (default) hidden property _Default of class Application is declared As String.
VBE - Object Browser - right click, tick Show Hiddden Members - Application.[_Default]
But the reason of such a declaration is unknown to me.
 
Upvote 0
It is because the (default) hidden property _Default of class Application is declared As String.
VBE - Object Browser - right click, tick Show Hiddden Members - Application.[_Default]
But the reason of such a declaration is unknown to me.
Thanks.

Yes, I know the Default Property is the Name Property which a string hence the returned vbString from the VarType function.
This makes the native VarType function very misleading.
As I mentioned in previous posts, one would expect that the Set keyword would coerce the variable into an object but that doesn't seem to reflect on the VarType result .. This is what I find difficult to accept.
 
  • Like
Reactions: ZVI
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,080
Members
448,943
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top