I'm trying to figure out how to create my own event using class modules in VBA.
I put together a simple example. You put the value in A1 and B1, and then re-activate the sheet to calculate the sum of the two, and then I was hoping the event would light up to warn about the calculation, but nothing happens.
I would be very grateful for the help in solving this example.
CCalc class module:
Dim m_wks As Worksheet Public Event BeforeCalc() Property Set Worksheet(wks As Worksheet) Set m_wks = wks End Property Public Sub Calc() Dim dVal1 As Double Dim dVal2 As Double With m_wks dVal1 = .Range("A1").Value dVal2 = .Range("B1").Value RaiseEvent BeforeCalc .Range("C1").Value = dVal1 + dVal2 End With End Sub
In the mGlobal module:
Public gCalc As cCalc
In the code behind Sheet1:
Private WithEvents calcEvent As cCalc Private Sub calcEvent_BeforeCalc() MsgBox "About to Calc!", vbInformation End Sub Private Sub Worksheet_Activate() Set gCalc = New cCalc Set gCalc.Worksheet = ActiveSheet gCalc.Calc End Sub
source share