vba Application.EnableEvents = False 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5169451/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 12:45:27  来源:igfitidea点击:

Application.EnableEvents = False not working

excelvbaprogramming-languages

提问by Radu Puspana

I don't seem to understand the strange behaviour of Excel 2007, and after a dozen solutions, I came to you asking for help.

我好像不太明白Excel 2007的奇怪行为,经过十几种解决方案,才来找你求助。

I have office 2007.

我有办公室 2007。

Here is my class module code :

这是我的类模块代码:

Public WithEvents App As Application
Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  MsgBox "event din app"
End Sub

Here is my InitializeAppObject module :

这是我的 InitializeAppObject 模块:

Dim X As New EventClassModule
Sub InitializeApp()
    Set X.App = Application

    MsgBox "am facut setarea"

End Sub

Here is my Sheet one code :

这是我的表一代码:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Not Application.Intersect(Target, Me.Range("a1:c10")) Is Nothing _
    Then
        Application.EnableEvents = False
        MsgBox "suntem in range"
        Application.EnableEvents = True
    Else
        MsgBox "nu suntem in range"
    End If
End Sub

Before changing any values on the grid, I execute the InitializeApp() procedure.

在更改网格上的任何值之前,我执行 InitializeApp() 过程。

From my understanding, the sheet event should be triggered first, then the workbook one, and then the application one. However this is not the case. It first fires up the workbook one, the the application one, and finally the sheet one.

根据我的理解,应该首先触发工作表事件,然后是工作簿事件,然后是应用程序事件。然而,这种情况并非如此。它首先启动工作簿一个,应用程序一个,最后是工作表一个。

  1. Is Excel malfunctioning or I got it wrong ?
  2. How can I execute just the sheet event? cause obviously the Application.EnableEvents = false is not doing any good from stopping the event from being triggered upstream.
  1. Excel 出现故障还是我弄错了?
  2. 如何仅执行工作表事件?原因很明显, Application.EnableEvents = false 对阻止上游触发事件没有任何好处。

Thank you so much in advance for all your help!

非常感谢您的帮助!

kind regards, radu

亲切的问候,拉杜

回答by Dick Kusleika

I think the problem may be that you're using the SelectionChange event in the sheet module. You're correct about the order of events. Here's how it goes - you're events have a *

我认为问题可能是您在工作表模块中使用了 SelectionChange 事件。你对事件的顺序是正确的。事情是这样的 - 你的活动有一个 *

Worksheet_Change
Workbook_SheetChange*
Application_SheetChange*

then assuming the selection moves after you enter something

然后假设选择在您输入内容后移动

Worksheet_SelectionChange*
Workbook_SheetSelectionChange
Application_SheetSelectionChange

Nothing in your code is triggering events, so putting message boxes in between EnableEvents doesn't achieve the desired result. All of the events are already queued up by the time the first event code runs. If instead, you had

您的代码中没有任何内容会触发事件,因此在 EnableEvents 之间放置消息框不会达到预期的结果。到第一个事件代码运行时,所有事件都已排队。如果相反,你有

Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    Application.EnableEvents = False
        Sh.Range("a1").Value = 1
    Application.EnableEvents = True
End Sub

Then the code that changed the value of A1 would not trigger any events. If you only want to run the Worksheet_Change event, you should delete the other event code. OK, it's probably there for a good reason. But whatever the logic is for when you run which event code needs to be in the procedure. For instance, if you only want to run the Worksheet_Change event on a particular worksheet name "Master", you would set it up like this

那么更改 A1 值的代码将不会触发任何事件。如果您只想运行 Worksheet_Change 事件,则应删除其他事件代码。好吧,这可能是有充分理由的。但是无论您运行时的逻辑是什么,程序中都需要包含哪些事件代码。例如,如果您只想在特定工作表名称“Master”上运行 Worksheet_Change 事件,您可以这样设置

Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range)
    If Sh.Name <> "Master" Then
        MsgBox "event din app"
    End If
End Sub

And the Worksheet_Change event code would be in master sheet's class module, so it would only respond to events on that page.

并且 Worksheet_Change 事件代码将在母版表的类模块中,因此它只会响应该页面上的事件。