Solved

Adding Protection to a Workbook by Spreadsheet with a Global command

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

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

705 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