Solved

Adding Protection to a Workbook by Spreadsheet with a Global command

Posted on 2014-11-01
2
78 Views
Last Modified: 2014-11-03
EE Pros,

I have a workbook that has a number of Worksheets that need to have protection on and off and on again.  The reason for this is that I need to protect the worksheets against users making changes and at the same time, I have a number of macros and outline layers that if I have protection on, it generates an error when it tries to fire the macro.

Here's my question; is there a macro or a way to put a short macro in a module that will turn protection on or off automatically based on a macro firing?  Even when I expand and contract an outline, I'm doing it with a macro (not the traditional way of selecting 1, 2, etc.).

Thank you in advance,

B.
0
Comment
Question by:Bright01
2 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 40417888
The code snippet below shows how you might toggle worksheet protection on and off. As written, the macro will turn protection on for all worksheets except those listed in the Select Case statement. If you run the macro a second time, it will turn protection off. Options are provided for using passwords.

The macro uses the UserInterfaceOnly parameter when protecting the worksheets. When UserInterfaceOnly is True, many macros will run successfully even if the worksheet is protected. It doesn't work in all cases, however, so you'll need to do some testing for each of your other macros to see whether you need to turn worksheet protection off or not.

The macro also includes an optional input (Variant, but a TRUE or FALSE input is expected) called bProtectionOn. If value is True, then the worksheets are protected. If False, they are unprotected. If value is not furnished by the calling macro, then worksheet protection is toggled according to value of Boolean bProtection.
Public bProtection As Boolean       'This statement must go in a regular module sheet above any subs or functions! True if protected, False if not.

Sub ToggleProtection(Optional bProtectionOn As Variant)
'Turn worksheet protection on or off
'Call ToggleProtection(True)        'Turns protection on
'Call ToggleProtection(False)       'Turns protection off
'Call ToggleProtection              'Turns protection on or off according to value of bProtection
'bProtectionOn is optional. If a value is furnished and is True, then worksheet protection is turned on. If False, worksheet protection is turned off. _
    If missing, then worksheet protection is toggled on and off according to value of Public variable bProtection. _
        In such cases, if bProtection is True before sub is called, then it will become False and protection will be turned off. _
        Ig bProtection is False before sub is called, then it will become True and protection will be turned on.
'Protecting a worksheet with UserInterfaceOnly:=True allows many macros to run without unprotecting the worksheet first. _
    Trial and error will determine whether you need to turn protection off for a particular macro.
Dim ws As Worksheet
Dim b As Boolean
Dim pswd As String
pswd = "MyPassword"     'If you use a password, put it here
If IsMissing(bProtectionOn) Then
    bProtection = Not bProtection
    b = bProtection
Else
    b = CBool(bProtectionOn)
End If

For Each ws In ActiveWorkbook.Worksheets
    Select Case UCase(ws.Name)
    Case "SHEET2", "SUMMARY", "DATA"    'These worksheets don't need to be protected/unprotected. List as many as you like (upper case only!).
    Case Else   'Toggle protection for all other worksheets
        If b = True Then
            ws.Protect pswd, UserInterfaceOnly:=True    'If you use a password
            'ws.Protect UserInterfaceOnly:=True          'If you don't use a password
        Else
            ws.Unprotect pswd                           'If you use a password
            'ws.Unprotect                                'If you don't use a password
        End If
    End Select
Next
End Sub

Open in new window

0
 

Author Closing Comment

by:Bright01
ID: 40419626
Thanks Brad!  Appreciate the quick and through response.  I'll have to do some testing with this..... you've given me an outstanding starting point.

Hope you are having a good time in SF.

B.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now