0% found this document useful (0 votes)
13 views

Run Procedures On Protected Worksheets

The document describes how to run macros on protected worksheets in Excel without unprotecting the worksheet. It explains that normally macros cannot run on protected worksheets and will result in an error. However, by using the UserInterfaceOnly argument when protecting the worksheet and setting it to True, it allows macros to run while still keeping the worksheet protected. When reopening the workbook, the entire worksheet will be protected again so the UserInterfaceOnly argument needs to be set back to True after reopening.

Uploaded by

mudassir2640
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Run Procedures On Protected Worksheets

The document describes how to run macros on protected worksheets in Excel without unprotecting the worksheet. It explains that normally macros cannot run on protected worksheets and will result in an error. However, by using the UserInterfaceOnly argument when protecting the worksheet and setting it to True, it allows macros to run while still keeping the worksheet protected. When reopening the workbook, the entire worksheet will be protected again so the UserInterfaceOnly argument needs to be set back to True after reopening.

Uploaded by

mudassir2640
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

The background color of each cell should have changed based on the number passed to the variable icolor,

which in turn passes this number to


Target.Interior.ColorIndex. The number that is passed is determined by
the line Case x To x. For example, if you enter the number 22 in any cell
within the range A1:A10, the number 15 is passed to icolor, and then
icolor (now having a value of 15) is passed to Target.Interior.ColorIndex,
making the cell gray. Target is always the cell that changed and, thus, fired
the code.

Run Procedures on Protected Worksheets

Hack #118

Excel macros are a great way to save time and eliminate errors. However,
sooner or later you might try to run your favorite Excel macro on a worksheet
that has been protected, with or without a password, resulting in a runtime
error. Avoid that problem with the following hack.

If you have ever tried to run an Excel macro on a worksheet thats been protected, you know that as soon as the worksheet is encountered, your macro
probably wont work and instead will display a runtime error.
One way to get around this is to use some code such as the following to
unprotect and then protect your worksheet:
Sub MyMacro( )
Sheet1.Unprotect Password:="Secret"
'YOUR CODE
Sheet1.Protect Password:="Secret"
End Sub

As you can see, the code unprotects Sheet1 with the password Secret, runs
the code, and then password-protects it again. This will work, but it has a
number of drawbacks. For one, the code could bug out and stop before it
encounters the Sheet1.Protect Password:="Secret" line of code. This, of
course, would leave your worksheet fully unprotected. Another drawback is
that you will need similar code for all macros and all worksheets.
Another way to avoid this problem is to use UserInterFaceOnly, which is an
optional argument of the Protect method that you can set to True. (The
default is False.) By setting this argument to True, Excel will allow all Excel
VBA macros to run on the worksheets that are protected with or without a
password.
However, if you use the Protect method with the UserInterfaceOnly argument set to True on a worksheet and then save the workbook, the entire
worksheet (not just the interface) will be fully protected when you reopen
the workbook. To set the UserInterfaceOnly argument back to True after the

You might also like