Unprotecting and then reprotecting a workbook

justanotheruser

Board Regular
Joined
Aug 14, 2010
Messages
96
Hi all,

Because the following code will be used by multiple end users, I would like to be able to protect the entire workbook so they can't delete/move/show sheets they shouldn't be able to. However, for the code below to work and show/hide sheets, the workbook needs to be unprotected and then reprotected. Therefore, I recorded a macro to see what the code is, and it's relatively simple so I inserted it as below.

E4 is a drop down list, and "Select Entity" (without "") is the first default option in the drop down list. I can select any of the things in the drop down list and it will do its magic, but if I return to "Select Entity" - the first option in the drop down list the workbook is unprotected for some reason - probably because it is the first value of the cell. Any suggestions?


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim LR As Long, i As Long
[COLOR="Red"]ActiveWorkbook.Unprotect[/COLOR]
If Target.Address(False, False) = "E4" Then
    If Target.Value = "Select Entity" Then
        For i = 1 To Worksheets.Count
            If Sheets(i).Name <> "Title" Then Sheets(i).Visible = False
        Next i
        Exit Sub
        End If
    Application.ScreenUpdating = False
    For i = 1 To Worksheets.Count
        If Sheets(i).Name <> "Title" Then Sheets(i).Visible = False
    Next i
    With Sheets("Mapping")
        LR = .Range("C" & Rows.Count).End(xlUp).Row
        For i = 3 To LR
            If Target.Value = .Range("C" & i).Value And .Range("D" & i).Value <> "" Then Sheets(.Range("D" & i).Value).Visible = True
        Next i
    End With
            Application.ScreenUpdating = True
        ' Used to show non-BU sheets for all entities, no matter of choice from dropdown box. Set all non-BU sheets to true to show them.
        Sheets("Other").Visible = True
        Sheets("BL_PS").Visible = True
End If
[COLOR="red"]ActiveWorkbook.Protect Structure:=True, Windows:=False[/COLOR]
End Sub

Thank you again! Today is question asking for me, hopefully next week will be helping! :D
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

Forum statistics

Threads
1,224,559
Messages
6,179,515
Members
452,921
Latest member
BBQKING

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top