2013-08-07 34 views
2

我在我的VBA代碼中有一些用戶定義的對象,並想知道是否有方法檢查是否有對象類型。有些東西就像用戶定義類型的VarType

Dim myObject as Variant 
Set myObject= New Employee 

    If(myObject isType Employee) 
      Do Something 
     Else 
      Do something else 

我在想VarType()函數,但它顯然不適用於用戶定義的類型。有什麼我可以使用的?

+0

'user-defined objects'您的意思是'classes'? –

+0

是的,我的意思是類。 – user2539552

回答

2

這樣做有兩種可能性。下面的代碼應該解釋一切。看到裏面的一些額外的評論:

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 
1

我相信你正在尋找TypeOf。使用上面的示例,它可以使用如下:

Dim myObject as Variant 
Set myObject= New Employee 

If TypeOf myObject is Employee Then 
    Do Something 
Else 
    Do SomethingElse 
End If