Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Adding Protection to a Workbook by Spreadsheet with a Global command

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 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