【VBA】クラス

定義方法

Class1

'プロパティ
Private p_Name As String

'プロパティプロシージャ
Property Get Name() As String
    Name = p_Name
End Property

Property Let Name(value As String)
    p_Name = value
End Property

'コンストラクタ
Private Sub Class_Initialize()
    p_Name = "Bob"
End Sub

'デコンストラクタ
Private Sub Class_Terminate()
    p_Name = ""
End Sub

' メソッド
Public Sub SayName()
    MsgBox Me.Name & "です"
End Sub

使用方法

    'インスタンス化
    Dim cls As New Class1
    
    'Let
    cls.Name = "高橋"
    
    'Get
    Debug.Print cls.Name
    
    'メソッド
    Call cls.SayName
    
    'インスタンスの破棄
    Set cls = Nothing