Excel vba - 禁用鼠标事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19082348/
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
Excel vba - Disable Mouse Events
提问by Matan_ma
I'm developing an Excel 2010workbook, in a manual formulas calculation mode.
我正在以手动公式计算模式开发Excel 2010工作簿。
file -> options ->formulas ->Workbook calculation -> manual
文件 -> 选项 -> 公式 -> 工作簿计算 -> 手册
However, I want some menu choices to cause recalculation of the workbook.
但是,我想要一些菜单选项来重新计算工作簿。
So I'm using the following code:
所以我使用以下代码:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Application.EnableEvents = False
If SOME_CONDITION
Application.Calculate
ActiveSheet.AutoFilter.ApplyFilter
End If
Application.EnableEvents = True
End Sub
The problem is that if I'm scrolling the middle mouse button, or clicking on a cell while this function is executed, the calculation is abruptly terminated before completion, which is unacceptable.
问题是,如果我在执行此函数时滚动鼠标中键或单击单元格,则计算在完成之前突然终止,这是不可接受的。
It seems that the line Application.EnableEvents=False
doesn't prevent the mouse events from being fired, and I wasn't able to find any alternative which will prevent this bug.
该行似乎Application.EnableEvents=False
并不能阻止鼠标事件被触发,而且我找不到任何可以阻止此错误的替代方法。
So what I need is either a way to block all events during the calculation, or somehow prevent the fired events from disrupting the calculation (as it is when the workbook formulas calculation is not manual).
所以我需要的是一种在计算过程中阻止所有事件的方法,或者以某种方式防止被触发的事件中断计算(就像工作簿公式计算不是手动的那样)。
I'd appreciate your help very much!
我非常感谢您的帮助!
Thanks.
谢谢。
Matan.
马坦。
回答by Portland Runner
Try this:
尝试这个:
Application.Interactive = False
While the macro is running. Turn it back to true when done.
当宏运行时。完成后将其恢复为 true。