ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Macro in a protected worksheet (https://www.excelbanter.com/excel-programming/359653-macro-protected-worksheet.html)

PA

Macro in a protected worksheet
 
One of our users has recorded two simple macros, one to apply a custom filter
and one to remove the filter. It is required that the worksheet cells with
formulas be protected, and they are scattered over a broad range.
When protected, the macros will not run. Security is set to medium, and
both of the Trusted Sources check boxes are check.

Is there a workaround for this behaviour?

Thanks for any suggestions
Paul

Ken Johnson

Macro in a protected worksheet
 
Hi Paul,
If the macros run but then produce an error then you should be able to
get them to work by including a line of code that unprotects the sheet.
This line will have to come before any other code lines that attempt to
change the sheet. If the protection was applied without a password then
just use...

ActiveSheet.Unprotect

If password eg 123 was used then use..

ActiveSheet.Unprotect(123)


After the macro code has done all its changes to the sheet the code can
reinstate the protection with the final line..

ActiveSheet.Protect 'if no password OR...

ActiveSheet.Protect(123) 'if password is 123

Ken Johnson


davesexcel[_98_]

Macro in a protected worksheet
 

You can unprotected sheet and protect sheet in the macro

ActiveSheet.Unprotect
'your code here
ActiveSheet.Protect


--
davesexcel


------------------------------------------------------------------------
davesexcel's Profile: http://www.excelforum.com/member.php...o&userid=31708
View this thread: http://www.excelforum.com/showthread...hreadid=535754


PA

Macro in a protected worksheet
 
Perfect, thank you.

"Ken Johnson" wrote:

Hi Paul,
If the macros run but then produce an error then you should be able to
get them to work by including a line of code that unprotects the sheet.
This line will have to come before any other code lines that attempt to
change the sheet. If the protection was applied without a password then
just use...

ActiveSheet.Unprotect

If password eg 123 was used then use..

ActiveSheet.Unprotect(123)


After the macro code has done all its changes to the sheet the code can
reinstate the protection with the final line..

ActiveSheet.Protect 'if no password OR...

ActiveSheet.Protect(123) 'if password is 123

Ken Johnson



Ken Johnson

Macro in a protected worksheet
 
You're welcome Paul.
Thanks for the feedback.

Ken Johnson



All times are GMT +1. The time now is 09:47 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com