vba Worksheet_Change Never FIRes
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11655327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Worksheet_Change Never FIres
提问by JamesD31
I am using VBA to alter Excel and have the following simple event handler inside the Sheet1:
我正在使用 VBA 来更改 Excel 并在 Sheet1 中有以下简单的事件处理程序:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox "HI"
End Sub
This never fires though, ever. I have tested in the Immediate Window:
不过,这永远不会触发。我在立即窗口中进行了测试:
?Application.EnableEvents
?Application.EnableEvents
which ultimately returns True, so it should fire. Does anyone know why this doesn't want to fire at all?
最终返回 True,所以它应该触发。有谁知道为什么这根本不想开火?
回答by Dick Kusleika
Do you have more than one sheet? Be aware that the sheet's CodeName property and the Name property (the name on the tab in Excel) aren't always the same. For instance, if the Project Explorer lists
你有不止一张纸吗?请注意,工作表的 CodeName 属性和 Name 属性(Excel 中选项卡上的名称)并不总是相同的。例如,如果项目资源管理器列出
Sheet2 (Sheet1)
Then Sheet1 is the Name and Sheet2 is the CodeName.
然后 Sheet1 是 Name,Sheet2 是 CodeName。
The easiest way to see if you have put the code in the wrong module is to right click on the sheet's tab and choose View Code. That will bring up the CodePane for that sheet and that's where your code should be.
查看是否将代码放入错误模块的最简单方法是右键单击工作表的选项卡并选择查看代码。这将显示该工作表的 CodePane,这就是您的代码所在的位置。
回答by Jeremy Caron
This question was posted a long time ago, but I recently had this same problem. If there are any other vba newbies out there struggling with this, try saving your workbook and re-opening it. After I did that, the Worksheet_Change Sub in my sheet fired as I expected.
这个问题是很久以前发布的,但我最近遇到了同样的问题。如果还有其他 vba 新手在为此苦苦挣扎,请尝试保存您的工作簿并重新打开它。在我这样做之后,我的工作表中的 Worksheet_Change Sub 按我的预期触发。
Perhaps there is a control to compile worksheet subs that I was missing???
也许有一个控件来编译我缺少的工作表子文件???
回答by whytheq
Usually used with Target
. Here's an example if you highlight M4 and change the value see what happens:
通常与Target
. 这是一个示例,如果您突出显示 M4 并更改值,看看会发生什么:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M" Then
MsgBox ("HI")
End If
End Sub
HEREis quite a nice posting about this event
这里有一个关于这个事件的很好的帖子
Adapting code from that post you can then do things like this
改编该帖子中的代码,然后您可以执行以下操作
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("$A:$V0")) Is Nothing Then Exit Sub
If Target.Address = "$A" Then
MsgBox ("A1")
ElseIf Target.Address = "$A" Then
MsgBox ("A2")
End If
End Sub
Ok - see comments below + I've just tested and your code should actually work. Try this? Does this also not work?
好的 - 请参阅下面的评论 + 我刚刚测试过,您的代码应该可以正常工作。尝试这个?这也不行吗?
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Cells) Then
MsgBox ("A1")
End If
End Sub