title: Using Class Modules in Excel VBA to Store and Compare Values of An Array When it Changes
body: <p>I chanced upon an answer to a previous post on the topic above. I have tried out the code recommended by Radek (as per the link provided below) and it works.</p>
  
  <p>I am wondering if someone could explain to me how to code works, i.e. how the <strong>MyIniVal</strong> and <strong>MyRange</strong> does not have the same value since the <strong>Initialize_MySheet</strong> procedure sets their value to be the same?</p>
  
  <p>Much thanks in advance!</p>
  
  <p><a href="https://stackoverflow.com/a/26738652/4814794">https://stackoverflow.com/a/26738652/4814794</a></p>
  
  <p>The code is:</p>
  
  <p>Class module called "Class1":</p>
  
  <pre><code>Public WithEvents MySheet As Worksheet
  Public MyRange As Range
  Public MyIniVal As Variant
  
  Public Sub Initialize_MySheet(Sh As Worksheet, Ran As Range)
      Set MySheet = Sh
      Set MyRange = Ran
      MyIniVal = Ran.Value
  End Sub
  Private Sub MySheet_Calculate()
  
  If MyRange.Value &lt;&gt; MyIniVal Then
      Debug.Print MyRange.Address &amp; " was changed from " &amp; MyIniVal &amp; " to " &amp; MyRange.Value
      StartClass
  End If
  
  End Sub
  </code></pre>
  
  <p>and in a normal module:</p>
  
  <pre><code>Dim MyClass As Class1
  
  Sub StartClass()
  Set MyClass = Nothing
  Set MyClass = New Class1
  MyClass.Initialize_MySheet ActiveSheet, Range("A2")
  End Sub
  </code></pre>