Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Excel from firing Worksheet_Change before Workbook_BeforeSave?

Tags:

excel

events

vba

Update: Issue Resolved A 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

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)?

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.

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...

like image 359
CamilB Avatar asked Jan 11 '12 12:01

CamilB


2 Answers

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().

The fix was easy. In Workbook_BeforeSave():

Application.EnableEvents = False
' Some final changes
Application.EnableEvents = True

And that was it :)

like image 127
CamilB Avatar answered Oct 05 '22 02:10

CamilB


Add a global flag variable.

Input a function that fires when key pressed and if CTRL + S it sets flag to true.

The worksheet_change event should short circuit if this flag is true.

And workbook_aftersave should change it back to false.

like image 41
kazmone1 Avatar answered Oct 05 '22 04:10

kazmone1