Reputation: 51
I have some user-defined objects in my VBA code and was wondering if there is a way to check was object type something is. Some thing like
Dim myObject as Variant
Set myObject= New Employee
If(myObject isType Employee)
Do Something
Else
Do something else
I was thinking the VarType() function, but it apparently won't work for user defined types. Is there something else I can use?
Upvotes: 2
Views: 1011
Reputation: 2087
I believe you're looking for TypeOf
. Using your example above, it can be used as follows:
Dim myObject as Variant
Set myObject= New Employee
If TypeOf myObject is Employee Then
Do Something
Else
Do SomethingElse
End If
Upvotes: 1
Reputation: 19067
There are two possibilities to do so. The following code should explain everything. See some additional comments inside:
Sub qTest()
Dim myObject As Variant
Set myObject = New Employee
'return results to Immediate
Debug.Print TypeName(myObject) '>> will return class name
Debug.Print TypeOf myObject Is Employee '>>will return true
'using with 'if statements', both will return true
If TypeName(myObject) = "Employee" Then
MsgBox "OK"
End If
If TypeOf myObject Is Employee Then
MsgBox "OK"
End If
End Sub
Upvotes: 2