VBA Before Save Event

jfr92

New Member
Joined
Dec 11, 2013
Messages
13
I use Excel 2010 64 bit W7, although I doubt that is important here. All the message boards say that a save by VBA should trigger a Before_Save event macro in the workbook being saved. It does not for me. I remember reading in an MSDN document on a related issue the offhand comment that Oh, by the way, a VBA Close will NOT trigger the Before_Close event but you can force code in the subject workbook related to any event trigger to be executed with a command that is particular to that event. I cannot remember the command nor find that same Microsoft document I refer to.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi - I have the same setup as you and the Before_Save event triggers for me. I just put the following test code into a blank workbook:

In Module:

Code:
Sub test()


ActiveWorkbook.Save


End Sub

Then in the ThisWorkbook object put the function:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


Debug.Print ("Triggered")


End Sub

Each time I run the test() macro I see the triggered getting printed in the immediate window showing it is being called.

Does this work for you?
 
Upvote 0
I guess I wasn't clear. The event trigger in book a does work for me when book a is saved manually or from vba code in book a but when book a is opened and modified and then saved by book b, the before save code in a is not executed. That's why I am looking for the vba instruction that can be put in b that tells event code in a to execute before b does the save. As I said, I saw a reference to such a command when I didn't need it and now can't find it again.
 
Upvote 0
Ok - I think I understand what you are trying to do.

You have a workbook (call it Workbook A) with some code in which opens another workbook (call it Workbook B). The code in Workbook A makes some edits to Workbook B and then you save. On the save of Workbook B which was initiated by code in Workbook A you want a Before_Save event to trigger.

If I have understood that right, I think you can do the following:

In Workbook A within the "ThisWorkbook" object (or any other object, just not an ordinary module), place the following code:

Code:
Dim WithEvents wb As Workbook


Sub test()


Set wb = Workbooks.Open("Book2.xlsx")
wb.SaveAs


wb.Close


End Sub


Private Sub wb_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


Debug.Print ("Triggered on Save")


End Sub

Does that work for you?

Dean.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,743
Messages
6,126,609
Members
449,321
Latest member
syzer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top