title: Getting VBA Function to auto update when a change is made on sheet - excel
body: <p>I have a VBA function which counts cells of a certain colour:</p>
  
  <pre><code>Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
  Dim indRefColor As Long
  Dim cellCurrent As Range
  Dim cntRes As Long
  
  Application.Volatile
  cntRes = 0
  indRefColor = cellRefColor.Cells(1, 1).Interior.Color
  For Each cellCurrent In rData
      If cellCurrent.Value &gt; 0 Then
          If indRefColor = cellCurrent.Interior.Color Then
          cntRes = cntRes + 1
          End If
      End If
  Next cellCurrent
  
  CountCellsByColor = cntRes
  End Function
  </code></pre>
  
  <p>My problem is that this doesn't update when changes are made on the sheet (only when F9 is pressed) - I'm new to VBA and would like it to auto update/execute the function when any change happens on the sheet. 
  I'm sure there are many ways to do this but a bit stuck on how to actually achieve it.</p>
  
  <p>Thanks in advance!</p>