Solved

Adding Protection to a Workbook by Spreadsheet with a Global command

Posted on 2014-11-01
2
89 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
[X]
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
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

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;…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

738 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