Remove and apply password when running a macro

Coppa

New Member
Joined
Mar 17, 2011
Messages
13
I have a password protect sheet, but want to give the user the option to run a macro, so i would like to build into the macro the password upfront to remove it, then run the macro, and then reapply the password. It is being applied on multiple sheets.

The first part works, where the password is removed, but the macro doesn't runs, and nor does it won't put the password back on. Please help

Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Unprotect Password:="1234"
Next wSheet

MACRO (it works perfectly before doing the password stuff)


For Each wSheet In Worksheets
wSheet.Protect Password:="1234"
Next wSheet

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
It works perfect for me.
Code:
[/FONT]
[FONT=Courier New]Option Explicit[/FONT]
[FONT=Courier New]Sub Protect()
Dim wSheet As Worksheet[/FONT]
[FONT=Courier New]For Each wSheet In Worksheets[/FONT]
[FONT=Courier New]wSheet.Unprotect Password:="1234"[/FONT]
[FONT=Courier New]Next wSheet[/FONT]
[FONT=Courier New]End Sub
Sub Unprotect()
Dim wSheet As Worksheet
For Each wSheet In Worksheets
wSheet.Protect Password:="1234"
Next wSheet[/FONT]
[FONT=Courier New]End Sub

what error are you receiving?
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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