lucavilla
asked on
Excel: how to save and restore sheet protection settings
(Excel 2010)
When programming in VBA I somethimes need to use methods that require to unprotect (unlock) the active sheet before being executed and to reprotect (lock) it when done.
For example the ShowAllData method.
Unfortunately the sheets are protected with different "allow" flags enabled/disabled and I want to leave those settings untouched by my script.
So... how can I save and restore the protection settings (flags)?
When programming in VBA I somethimes need to use methods that require to unprotect (unlock) the active sheet before being executed and to reprotect (lock) it when done.
For example the ShowAllData method.
Unfortunately the sheets are protected with different "allow" flags enabled/disabled and I want to leave those settings untouched by my script.
So... how can I save and restore the protection settings (flags)?
ASKER
No, they modify the autofilter but it can't modify the autofilter if sheet si protected.
A litte bit confused, did you read this, if please write your thread much clear
http://office.microsoft.com/en-us/excel-help/password-protect-worksheet-or-workbook-elements-HP010078580.aspx
http://office.microsoft.com/en-us/excel-help/password-protect-worksheet-or-workbook-elements-HP010078580.aspx
ASKER
The problem is very easy.
Set an autofilter and filter something.
Then execute this code:
if sheet is unprotected -> it removes your filter
if sheet is protected -> it crashes on the ShowAllData line with a 1004 error
I would to avoid the crash (keeping the same logic).
PS: manually removing and reenabling the protection is not an option. I've to use it often and over many files of other users.
Set an autofilter and filter something.
Then execute this code:
Sub test1()
With ActiveSheet
If .AutoFilterMode Then
If .FilterMode Then
.ShowAllData
End If
Else
If .FilterMode Then
.ShowAllData
End If
End If
End With
End Sub
Results:if sheet is unprotected -> it removes your filter
if sheet is protected -> it crashes on the ShowAllData line with a 1004 error
I would to avoid the crash (keeping the same logic).
PS: manually removing and reenabling the protection is not an option. I've to use it often and over many files of other users.
When you open the workbook, unprotect the worksheet, then reprotect it with UserInterfaceOnly parameter set to True. You can then run your macro while the worksheet is protected.
Worksheets("Sheet1").Prote ct Password:="somepassword", UserInterfaceOnly:= True
Worksheets("Sheet1").Prote
ASKER
Wow thanks byundt! it worked!
If it's not possible (as alternative) to save and restore protection settings I will change the protection on all the files (a lot) with your "UserInterfaceOnly:= True" method.
If it's not possible (as alternative) to save and restore protection settings I will change the protection on all the files (a lot) with your "UserInterfaceOnly:= True" method.
The UserInterfaceOnly property is not saved with the file, unfortunately. It must be set each time you open the file, as well as each time you apply protection.
The UserIntefaceOnly trick to allow macros to work without removing sheet protection works in many cases, but not all. But when it does work, as in your current situation, it is a very nice trick indeed.
Brad
The UserIntefaceOnly trick to allow macros to work without removing sheet protection works in many cases, but not all. But when it does work, as in your current situation, it is a very nice trick indeed.
Brad
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Wait, the big problem is that I'm in a company where there are many Excel files with different protection settings.
I just want to use the ShowAllData command in a macroes in my PERSONAL.XLSB over all these files without disrupting their protection settings.
Is it really impossible to detect the original settings and restore them at the end of my macroes?
I just want to use the ShowAllData command in a macroes in my PERSONAL.XLSB over all these files without disrupting their protection settings.
Is it really impossible to detect the original settings and restore them at the end of my macroes?
Is it really impossible to detect the original settings and restore them at the end of my macros?It is quite possible to detect the original settings and restore them at the end of the macro. That's exactly what the code in my last post is doing.
As written, it runs on Excel 2013 and restores all the protection features supported by that version of Excel. Some of those features, however, are not available on earlier versions of Excel. These unavailable features will cause run-time errors when the code is run on those earlier versions.
You will need to determine the oldest version of Excel that will be in use by one of your macro users. You then have a choice of either simplifying the code to support just that oldest version, or testing the version in the code and executing different statements for each one.
ASKER
byundt I don't understand because I see a single sub.
What's the part that reads/saves the original settings and what's the part that restores them?
What's the part that reads/saves the original settings and what's the part that restores them?
Statements 8 to 12 capture the original settings. Statements 15 to 31 reapply them along with UserInterfaceOnly:=True
ASKER
ah, great!
now I try it, adapting it to the limited settings of Excel 2010
now I try it, adapting it to the limited settings of Excel 2010
You may find that recording a macro will expose the settings that are present in your version of Excel for comparison to those in Excel 2013.
ASKER
impressive byundt!
Your sub works perfectly without modifications on my Excel 2010!
I love you!!!
I would to give you 5000 points!
Your sub works perfectly without modifications on my Excel 2010!
I love you!!!
I would to give you 5000 points!
http://www.mrexcel.com/forum/excel-questions/228818-showalldata.html