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?
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.
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?
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.
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.