簡體   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