can I start a macro when the close button is pressed?

jsambrook

Board Regular
Joined
Feb 1, 2010
Messages
214
I want to get the user to enter data into a version control before closing the file. Can i get a macro to trigger as the file is closed?

thanks
 

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

ALT F11 to open the code editor
On the left tree, go to ThisWorkbook; double click to open the code pane

At the top of the code pane, with the left drop down select Workbook
With the right drop down select BeforeClose

Code:
Private Sub Workbook_BeforeClose
 
End Sub

Anything you put in there will run when the workbook closes.
 
Upvote 0
Although I'm sure it will be easy once I find this drop down on the left? I've got the code open but cant find a left drop down? Can you point me a bit closer pls?
Many many thanks
 
Upvote 0
Thanks Tweedle. I'm still a bit confused as to where I should put this code? I cant seem to get it to trigger when I close.
thanks
 
Upvote 0
I've managed to get the macro to work, apart from I cant update the version loge as it closes the file as soon as the message box it slicked. How do I cancel the close?


Private Sub Workbook_BeforeClose(cancel As Boolean)

x = MsgBox("Have you update the Version Log", vbYesNo)
If x = 6 Then Exit Sub
Sheets("Version Log").Select
Range("B5").Select
MsgBox ("Update Version Log")
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,574
Messages
6,179,626
Members
452,933
Latest member
patv

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