Run Protect Macro on Close

samsilverman

Board Regular
Joined
Nov 9, 2009
Messages
176
Excel 07. I have unprotect and protect macros that run when I click on two different buttons. Sometimes, the tech (end user) forgets to reprotect the workbook before closing it. How can I get the macro I already have (which protects 7 worksheets with different passwords) to run automatically upon closing?
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Use the workbook before close event to call the protect macro
 
Upvote 0
Try like this in the ThisWorkbook module

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Call MyMacro
End Sub
 
Upvote 0
Try like this in the ThisWorkbook module

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

So, does the name of the macro I want to run go in place of "MyMacro" in your code above? Because I can't find the name of my macro in the Macros List. When I look at the MEO sheet in VBA, the macro is there with the heading:
Private Sub CommandButton3_Click()

Help?
 
Upvote 0
Instead of MyMacro try CommandButton3_Click

If that doesn't work, copy the code from CommandButton3_Click (without the first and last lines) and paste it into the WorkbookBeforeSave sub instead of the Call statement.
 
Upvote 0
Instead of MyMacro try CommandButton3_Click

If that doesn't work, copy the code from CommandButton3_Click (without the first and last lines) and paste it into the WorkbookBeforeSave sub instead of the Call statement.

Thanks VoG. Works Great!! I took the
copy the code from CommandButton3_Click (without the first and last lines) and paste it into the WorkbookBeforeSave sub instead of the Call statement.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,225,477
Messages
6,185,210
Members
453,283
Latest member
Shortm88

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