简体   繁体   English

在单元格更改时自动执行 Excel 宏

[英]automatically execute an Excel macro on a cell change

How can I automatically execute an Excel macro each time a value in a particular cell changes?每次特定单元格中的值发生变化时,如何自动执行 Excel 宏?

Right now, my working code is:现在,我的工作代码是:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("H5")) Is Nothing Then Macro
End Sub

where "H5" is the particular cell being monitored and Macro is the name of the macro.其中"H5"是被监控的特定单元, Macro是宏的名称。

Is there a better way?有没有更好的办法?

Your code looks pretty good.你的代码看起来很不错。

Be careful, however, for your call to Range("H5")<\/code> is a shortcut command to Application.Range("H5")<\/code> , which is equivalent to Application.ActiveSheet.Range("H5")<\/code> .但是请注意,调用Range("H5")<\/code>是Application.Range("H5")<\/code>的快捷命令,它等效于Application.ActiveSheet.Range("H5")<\/code> 。 This could be fine, if the only changes are user-changes -- which is the most typical -- but it is possible for the worksheet's cell values to change when it is not the active sheet via programmatic changes, eg VBA.这可能很好,如果唯一的更改是用户更改 - 这是最典型的 - 但是当工作表的单元格值不是活动工作表时,它可能会通过编程更改(例如 VBA)进行更改。

With this in mind, I would utilize Target.Worksheet.Range("H5")<\/code> :考虑到这一点,我将使用Target.Worksheet.Range("H5")<\/code> :

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Target.Worksheet.Range("H5")) Is Nothing Then Macro
End Sub

Handle the Worksheet_Change event or the Workbook_SheetChange event.处理Worksheet_Change事件或Workbook_SheetChange事件。

The event handlers take an argument "Target As Range", so you can check if the range that's changing includes the cell you're interested in.事件处理程序采用参数“Target As Range”,因此您可以检查正在更改的范围是否包括您感兴趣的单元格。

I spent a lot of time researching this and learning how it all works, after really messing up the event triggers.在真正搞砸了事件触发器之后,我花了很多时间研究这个并了解它是如何工作的。 Since there was so much scattered info I decided to share what I have found to work all in one place, step by step as follows:由于有这么多分散的信息,我决定将我发现的所有内容分享在一个地方,一步一步如下:

1) Open VBA Editor, under VBA Project (YourWorkBookName.xlsm) open Microsoft Excel Object and select the Sheet to which the change event will pertain. 1) 打开 VBA 编辑器,在 VBA 项目 (YourWorkBookName.xlsm) 下打开 Microsoft Excel 对象并选择更改事件将涉及的工作表。

2) The default code view is "General." 2) 默认代码视图是“常规”。 From the drop-down list at the top middle, select "Worksheet."从顶部中间的下拉列表中,选择“工作表”。

3) Private Sub Worksheet_SelectionChange is already there as it should be, leave it alone. 3) Private Sub Worksheet_SelectionChange 应该已经存在,不要管它。 Copy/Paste Mike Rosenblum's code from above and change the .Range reference to the cell for which you are watching for a change (B3, in my case).从上面复制/粘贴 Mike Rosenblum 的代码并将 .Range 引用更改为您正在观察更改的单元格(在我的情况下为 B3)。 Do not place your Macro yet, however (I removed the word "Macro" after "Then"):但是,不要放置您的宏(我在“然后”之后删除了“宏”这个词):

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("H5")) Is Nothing Then
End Sub

or from the drop-down list at the top left, select "Change" and in the space between Private Sub and End Sub, paste If Not Intersect(Target, Me.Range("H5")) Is Nothing Then或从左上角的下拉列表中选择“更改”,然后在 Private Sub 和 End Sub 之间的空白处粘贴If Not Intersect(Target, Me.Range("H5")) Is Nothing Then

4) On the line after "Then" turn off events so that when you call your macro, it does not trigger events and try to run this Worksheet_Change again in a never ending cycle that crashes Excel and/or otherwise messes everything up: 4)在“然后”之后的行上关闭事件,这样当您调用宏时,它不会触发事件并尝试在一个永无止境的循环中再次运行此 Worksheet_Change,这会导致 Excel 崩溃和/或以其他方式搞砸一切:

Application.EnableEvents = False

5) Call your macro 5)调用你的宏

Call YourMacroName

6) Turn events back on so the next change (and any/all other events) trigger: 6)重新打开事件,以便下一个更改(和任何/所有其他事件)触发:

Application.EnableEvents = True

7) End the If block and the Sub: 7) 结束 If 块和 Sub:

    End If
End Sub

The entire code:整个代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Me.Range("B3")) Is Nothing Then
        Application.EnableEvents = False
        Call UpdateAndViewOnly
        Application.EnableEvents = True
    End If
End Sub

This takes turning events on/off out of the Modules which creates problems and simply lets the change trigger, turns off events, runs your macro and turns events back on.这会将事件从模块中打开/关闭,这会产生问题,并且只是让更改触发、关闭事件、运行宏并重新打开事件。

I prefer this way, not using a cell but a range我更喜欢这种方式,不使用单元格,而是使用范围

    Dim cell_to_test As Range, cells_changed As Range

    Set cells_changed = Target(1, 1)
    Set cell_to_test = Range( RANGE_OF_CELLS_TO_DETECT )

    If Not Intersect(cells_changed, cell_to_test) Is Nothing Then 
       Macro
    End If

I have a cell which is linked to online stock database and updated frequently.我有一个链接到在线股票数据库并经常更新的单元格。 I want to trigger a macro whenever the cell value is updated.我想在单元格值更新时触发一个宏。

I believe this is similar to cell value change by a program or any external data update but above examples somehow do not work for me.我相信这类似于程序或任何外部数据更新的单元格值更改,但以上示例对我不起作用。 I think the problem is because excel internal events are not triggered, but thats my guess.我认为问题在于没有触发excel内部事件,但那是我的猜测。

I did the following,我做了以下,

Private Sub Worksheet_Change(ByVal Target As Range) 
  If Not Intersect(Target, Target.Worksheets("Symbols").Range("$C$3")) Is Nothing Then
   'Run Macro
End Sub

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

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