在小区改变自动执行的Excel宏 [英] automatically execute an Excel macro on a cell change

查看:140
本文介绍了在小区改变自动执行的Excel宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能自动在特定的细胞变化执行Excel宏每次价值?

How can I automatically execute an Excel macro each time a value in a particular cell changes?

现在,我的工作code是:

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

其中,H5是被监视的特定细胞和是宏的名称。

where "H5" is the particular cell being monitored and Macro is the name of the macro.

有没有更好的办法?

推荐答案

您code相pretty不错。

Your code looks pretty good.

要小心,但是,对于您的来电范围(H5)是一个快捷命令Application.Range(H5),这相当于Application.ActiveSheet.Range(H5)。这可能是罚款,如果唯一变化是用户的变化 - 这是最典型的 - 但它是可能的工作表的单元格值改变时,它不是通过编程方式的改变,例如活动工作表VBA。

Be careful, however, for your call to Range("H5") is a shortcut command to Application.Range("H5"), which is equivalent to Application.ActiveSheet.Range("H5"). 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, e.g. VBA.

考虑到这一点,我将利用Target.Worksheet.Range(H5):

With this in mind, I would utilize Target.Worksheet.Range("H5"):

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

或者你可以使用Me.Range(H5),如果事件处理程序是有问题的工作表中的code页面上(通常是这样):

Or you can use Me.Range("H5"), if the event handler is on the code page for the worksheet in question (it usually is):

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

希望这有助于...

Hope this helps...

这篇关于在小区改变自动执行的Excel宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆