BeforeClose not running

TheMadTree

New Member
Joined
Mar 23, 2015
Messages
25
Good afternoon All,

My first post to MrExcel B)

I want a macro that password protects all sheets in the workbook called 'Potato' when the workbook is closed.
For some reason it doesn't work and I can't figure our why :s

Can anyone help me?

This is what I've written:

Option Explicit
Sub Protectsheets_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
Dim pwd As String
pwd = Orange30
For Each ws In Workbooks("Potato.xlsm").Worksheets
ws.Protect Password:=pwd, DrawingObjects:=True, Contents:=True, Scenarios:=True, AllowSorting:=True
Next ws
If Me.Saved = False Then Cancel = True
End Sub
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Welcome to the Board!

The event needs to read:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

And the code needs to go in the ThisWorkbook module.

HTH,
 
Upvote 0

Forum statistics

Threads
1,214,638
Messages
6,120,674
Members
448,977
Latest member
moonlight6

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