Home > Net >  How to use Excel VBA "Worksheet_Calculate" function for a range of cells
How to use Excel VBA "Worksheet_Calculate" function for a range of cells

Time:05-09

I want my macro to activate ONLY when a calculated cell in a SPECIFIED range changes. At the moment the macro activates whenever any cell on the sheet is calculated.

For example, how would I alter the following code so that Macro1 only activates when a cell in range(A1:A5) changes due to a calculation, and not when cells in any other ranges are recalculated? Any guidance would be much appreciated.

Private Sub Worksheet_Calculate()
Macro1
End Sub

CodePudding user response:

This should do it...

Private Sub Worksheet_Calculate()
    Static last, test
    
    test = [sum(a1:a5)]
    
    If Not IsError(test) Then
        If last <> test Then
            last = test
            Macro1
        End If
    End If
End Sub
  • Related