Answers for "vba event variable change"

VBA
0

vba event variable change

' Adds a user defined trigger (OnChange event) on a class attribute
' Msgbox when Name (clsMyClass) is changed
' Add class module clsMyClass:
Private WithEvents mName As clsWithEvents
Private Sub mName_OnChange(ByVal pValue As String)
    MsgBox "Value changed to " & pValue
End Sub
Public Property Get Name() As clsWithEvents
    Set Name = mName
End Property
Public Property Let Name(pClsName As clsWithEvents)
    Set mName = pClsName
End Property

'Add class module clsWithEvents
Public Event OnChange(ByVal pValue As String)
Private mCurrentValue As String
Public Property Get Value() As String
    Value = mCurrentValue
End Property
Public Property Let Value(ByVal pValue As String)
    If mCurrentValue <> pValue Then RaiseEvent OnChange(pValue)
    mCurrentValue = pValue
End Property

' Test in a module:
Sub TestMe()
    Dim cMyClass As New clsMyClass
    Dim cName As New clsWithEvents
    cMyClass.Name = cName
    cName.Value = "New value"   ' Triggers a msgbox
End Sub
Posted by: Guest on January-23-2021

Code answers related to "VBA"

Browse Popular Code Answers by Language