vba Application.EnableEvents = true 每次都返回 false
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19355257/
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
Application.EnableEvents = true every time returns false
提问by udaya726
I have opened my excel sheet and then opend the VBA editor.In the immediate window I execute the following command.
我打开了我的 excel 表,然后打开了 VBA 编辑器。在即时窗口中,我执行了以下命令。
?Application.EnableEvents = True
It returns False . But If I opened new excel sheet and type the command it returns True. Any reason for that
它返回 False 。但是,如果我打开新的 Excel 工作表并输入命令,它将返回 True。任何原因
回答by Allen Gould
I don't believe the command is doing what you think it is.
我不相信该命令正在按照您的想法行事。
Remember that VBA overloads the = operator to be both assignment and comparison, and uses context to decide which is which.
请记住,VBA 将 = 运算符重载为赋值和比较,并使用上下文来决定哪个是哪个。
The ? Operator makes VBA believe that you're asking "are these two the same", rather than "set this to that".
这 ?运算符让 VBA 相信您在问“这两个相同吗”,而不是“将其设置为那个”。
Some example code:
一些示例代码:
Application.EnableEvents = False
? Application.EnableEvents
False
? Application.EnableEvents = True
False
? Application.EnableEvents = False
True
? Application.EnableEvents
True
Application.EnableEvents = True
? Application.EnableEvents
True
? Application.EnableEvents = True
True
? Application.EnableEvents = False
False
? Application.Enableevents
True
Note that none of the ? commands change the value - all of them merely report whether the comparisonis true or not, without adjusting the variable. Only a flat assignment will do it.
请注意,没有一个 ? 命令更改值 - 所有这些命令都只报告比较是否为真,而不调整变量。只有平面分配才能做到这一点。