title: Creating A Custom Event With VBA
body: <p>I'm trying struggling to understand how to create a custom event using class modules in VBA.</p>
  
  <p>I've put together the simple following example. You put a value in A1 and B1 and then re activate the sheet to calculate the sum of the two and then I hoped an event would fire to warn of calculation, but nothing happens.</p>
  
  <p>I'd be very grateful for any help solving this example.</p>
  
  <p>Class module cCalc:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>In a module mGlobal:</p>
  
  <pre><code>Public gCalc As cCalc
  </code></pre>
  
  <p>In the code behind Sheet1:</p>
  
  <pre><code>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
  </code></pre>