Protecting a Sheet

ERed1

Board Regular
Joined
Jun 26, 2019
Messages
104
Hi all,

I have a spreadsheet with macros that I wrote. I have to give it to some colleagues and need to protect the sheet. When I protect it normally it makes the code not work. Has anyone protected a macro enabled worksheet?

Thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Write some lines into your code, to un-protect the sheet, then re-protect, after the code's run:
Code:
Sub unprotect_sheet()
Me.Unprotect "password"
' All your code here...
Me.Protect "password"
End Sub
Obviously change the password to the one you've used, and if you don't want your users having a sneaky peek into your code, to find the hard-coded sheet's password, then password-protect your project, too.

The "Me" keyword will only work if you put the code into the actual sheet's code module. If the code's anywhere else, you'll need to refer to the sheet longhand "Sheets("Sheet1") or "Activesheet" etc.
 
Last edited:
Upvote 0
Hi all,

I have a spreadsheet with macros that I wrote. I have to give it to some colleagues and need to protect the sheet. When I protect it normally it makes the code not work. Has anyone protected a macro enabled worksheet?

Thanks


Is your code going to affect the protected sheet? If so, you will need to programmatically unprotect it prior to your code running and then programmatically re-protect it after code execution.
 
Upvote 0
You're welcome. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,743
Messages
6,180,687
Members
452,994
Latest member
Janick

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