VBA在类内部获取实例名称

时间:2018-06-21 09:18:57

标签: vba

Sub Main()
    Dim test As New Class1
End sub

Class1:

Private Sub Class_Initialize()
    msgbox(Name_of_Class_Instance)   
End Sub

我希望msgbox显示“测试”

可以在VBA中完成吗?

1 个答案:

答案 0 :(得分:0)

据我了解,您正在尝试获取在模块中声明的类实例名称

首先,您需要创建一个如下所示的类。

Public classname As String

'Below method is going to get the value from module.

Public Sub Class_Initialize()
MsgBox classname
End Sub

第二,创建如下所示的模块。

Sub getname()

Dim test As Class1
Set test = New Class1

'Here we are passing the class name as 'test' and executing the 'Class_Initialize' method

With test
.classname = "test"
.Class_Initialize
End With

End Sub

现在,您将获得该类的实例名称。