每当单元格更改时,excel VBA都会自动运行宏 [英] excel VBA run macro automatically whenever a cell is changed

查看:133
本文介绍了每当单元格更改时,excel VBA都会自动运行宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以让 Excel 在单元格更改时自动执行宏?

Is there a simple way to get Excel to automatically execute a macro whenever a cell is changed?

有问题的单元格将在 Worksheet("BigBoard").Range("D2")

事实证明,我认为简单的 Google 查询会变得更加复杂 - 每个涉及的样本都包含相交(无论是什么)或颜色格式或任何其他看起来不相关的事物.

What I thought would be a simple Google inquiry is proving to be more complicated - every sample involved intersects (whatever those are) or color formatting or any other number of things that appear to be irrelevant.

推荐答案

是的,这可以通过使用工作表事件来实现:

Yes, this is possible by using worksheet events:

在 Visual Basic 编辑器中,双击左上角树中的工作表名称,打开您感兴趣的工作表(即BigBoard").将以下代码放入模块中:

In the Visual Basic Editor open the worksheet you're interested in (i.e. "BigBoard") by double clicking on the name of the worksheet in the tree at the top left. Place the following code in the module:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Me.Range("D2")) Is Nothing Then Exit Sub
        Application.EnableEvents = False 'to prevent endless loop
        On Error Goto Finalize 'to re-enable the events      
        MsgBox "You changed THE CELL!"
    End If
Finalize:        
    Application.EnableEvents = True
End Sub

这篇关于每当单元格更改时,excel VBA都会自动运行宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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