How to run a macro after user has chosen save "yes"/"no" upon closing file

skylined

New Member
Joined
Sep 25, 2009
Messages
1
Hi all,

I'm making my first moves in the excel macro world, and having the following difficulty:

I have an excel sheet in which I build in some checks and macros, so therefore I need to ensure that users execute macros when opening the file. I looked around on the internet and found that this is best done by hiding all sheets upon closing of the file. This way, when a user opens it without executing macros, he does not see his file.

The formula:
Sub Hide_All()
Sheet1.Visible = xlSheetVeryHidden
Sheet4.Visible = xlSheetVisible
End Sub


And executed in:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Hide_All
End Sub


Problem is now: say a user works in the sheet, saves it (e.g. using save as) and clicks on the "X" on the right top side of the screen to close the file, he/she gets the question "do you want to save yes/no/cancel".

When taking "yes" or "cancel": no problem.
But when choosing "no", the excel sheet does not log the fact that the sheets have been hidden. And so the next time he/she opens without executing macros, the files that are supposed to be hidden are visible.

So in brief: how can I execute the hide macro even if the user selects "no" as save option upon closing? (perhaps a mandatory save as?)

 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hello and welcome to MrExcel.

Try

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.DisplayAlerts = False
Hide_All
ThisWorkbook.Save
Application.DisplayAlerts = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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