Solved

Adding Protection to a Workbook by Spreadsheet with a Global command

Posted on 2014-11-01
2
76 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

911 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

16 Experts available now in Live!

Get 1:1 Help Now