简体   繁体   English

如何对一系列单元格使用 Excel VBA“Worksheet_Calculate”函数

[英]How to use Excel VBA "Worksheet_Calculate" function for a range of cells

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?例如,我将如何更改以下代码,以便Macro1仅在Range(A1:A5)中的单元格因计算而更改时激活,而不是在重新计算任何其他范围中的单元格时激活? Any guidance would be much appreciated.任何指导将不胜感激。

Private Sub Worksheet_Calculate()
    Macro1
End Sub

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM