繁体   English   中英

单元格值更改时自动运行Excel VBA宏

[英]Automatically run Excel VBA macro when cell value changes

我需要一个在单元格A1中的值更改时调用另一个宏的宏。 目前,我正在使用以下代码,但似乎每次页面重新计算时都会调用“ pageupdate”子项,而不仅仅是A1中的值实际上从0更改为1时。

谁了解“ Worksheet_Calculate”子类型或if语句背后的逻辑,就会遇到这个问题吗?

Private Sub Worksheet_Calculate()
    '|-------------------------------------------------|
    '|        Run Pageupdate                           |
    '|-------------------------------------------------|

    'If cell A1 recalculates affected by a change in the sheet/s, then this macro runs the 'PageUpdate' Macro.
    'The point of this is to prevent the PageUpdate running when it doesn't need to.

    Static OldVal As Variant
    If Range("A1").Value <> OldVal Then
        OldVal = Range("A1").Value
        Call PageUpdate
    End If
End Sub
  • 将以下子项作为宏添加到希望受到更新影响的工作表中,并将代码放入此宏中。 您可以在这里找到解决方案

    专用子工作表_更改(按ByVal目标作为范围)将昏暗KeyCell作为范围

     ' The variable KeyCells contains the cells that will ' cause an alert when they are changed. Set KeyCells = Range("A1:A1") If Not Application.Intersect(KeyCells, Range(Target.Address)) _ Is Nothing Then ' Display a message when one of the designated cells has been ' changed. ' Place your code here. 'MsgBox "Cell " & Target.Address & " has changed." End If 

    结束子

暂无
暂无

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

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