vba 如何在 Workbook_BeforeSave 之前阻止 Excel 触发 Worksheet_Change?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8819394/
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
How to stop Excel from firing Worksheet_Change before Workbook_BeforeSave?
提问by CamilB
Update: Issue ResolvedA colleague of mine was changing a cell during Workbook_BeforeSave()
without disabling events, therefore triggering Worksheet_Change()
. Yes, silly, but at least it's our fault, not Excel's
更新:问题已解决 我的一位同事在Workbook_BeforeSave()
没有禁用事件的情况下更改了一个单元格,因此触发了Worksheet_Change()
. 是的,愚蠢,但至少这是我们的错,而不是 Excel 的错
I've noticed that whenever I hit Ctrl+S in Excel, the Worksheet_Change()
is fired before Workbook_BeforeSave()
. Is it possible to supress this behaviour using VBA code, but without supressing all events (i.e. without Application.EnableEvents = false
)?
我注意到,每当我在 Excel 中按 Ctrl+S 时,Worksheet_Change()
都会在Workbook_BeforeSave()
. 是否可以使用 VBA 代码抑制这种行为,但不抑制所有事件(即没有Application.EnableEvents = false
)?
This happens regardless of what I'm doing. I've read about someone having a similar issue with ComboBoxes, but I'm not editing ComboBoxes, yet Worksheet_Change()
fires always before saving.
无论我在做什么,都会发生这种情况。我读过有人对 ComboBox 有类似的问题,但我没有编辑 ComboBox,但Worksheet_Change()
在保存之前总是触发。
Any ideas? I'm only trying to figure out how to bypass some code inside Worksheet_Change()
when the document is saved, because that code is only supposed to be executed when the user actually changes something, not when the workbook is saved. Saving is by no means changing...
有任何想法吗?我只是想弄清楚如何在Worksheet_Change()
保存文档时绕过内部的一些代码,因为该代码只应该在用户实际更改某些内容时执行,而不是在保存工作簿时执行。储蓄绝不是改变...
回答by CamilB
It was a coding mistake on our side:
这是我们这边的编码错误:
A colleague of mine was changing a cell during Workbook_BeforeSave() without disabling events, therefore triggering Worksheet_Change().
我的一位同事在 Workbook_BeforeSave() 期间更改了一个单元格而没有禁用事件,因此触发了 Worksheet_Change()。
The fix was easy. In Workbook_BeforeSave():
修复很容易。在 Workbook_BeforeSave() 中:
Application.EnableEvents = False
' Some final changes
Application.EnableEvents = True
And that was it :)
就是这样:)
回答by kazmone1
Add a global flag variable.
添加全局标志变量。
Input a function that fires when key pressed and if CTRL + S it sets flag to true.
输入一个在按下键时触发的函数,如果 CTRL + S 它将标志设置为 true。
The worksheet_change event should short circuit if this flag is true.
如果此标志为真,则 worksheet_change 事件应该短路。
And workbook_aftersave should change it back to false.
并且 workbook_aftersave 应该将其改回 false。