My ".unprotect" code runs fine on one piece of code and fails on the other and I cannot figure out why

morrisps

New Member
Joined
Jul 17, 2017
Messages
15
I am working on a spreadsheet to track maintenance, it needs to be protected so that none of the formulas or formats are messed up by the users.

Anyway I have this piece of Code that changes the sheet based on a a choice earlier on, and it runs perfectly when the sheet is protected or not:
Code:
 If Target.Address = "$K$22" Then    'hides or unhides the appropriate columns for phase 1 or 3 power.    ActiveSheet.Unprotect Password:="ex03740"
    If Target.Value = "1" Then
            Range("A25", "A27").EntireRow.Hidden = False
            Range("A34", "A36").EntireRow.Hidden = False
            Range("A28", "A32").EntireRow.Hidden = True
            Range("A37", "A41").EntireRow.Hidden = True
        Else
            Range("A25", "A27").EntireRow.Hidden = True
            Range("A34", "A36").EntireRow.Hidden = True
            Range("A28", "A32").EntireRow.Hidden = False
            Range("A37", "A41").EntireRow.Hidden = False
        End If
        ActiveSheet.Protect Password:="ex03740"
    End If

And then I have this piece of code:
Code:
If Target.Address = "$U$42" Then    'hides or unhides the rows for battery dischrage test
    ActiveSheet.Unprotect Password:="ex03740"
    If Target.Value = "Yes" Then
            ActiveSheet.Range("A48", "A50").EntireRow.Hidden = False
            Sheets("Discharge Test").Visible = True
        Else
            ActiveSheet.Range("A48", "A50").EntireRow.Hidden = True
            Sheets("Discharge Test").Visible = False
        End If
        ActiveSheet.Protect Password:="ex03740"
    End If

which is especially the same, but tells me the change is trying to be made on a protected sheet even though I have the unprotect line there.

Now I do realize that I have alterations to another sheet in that piece of code. However, I have tested it with the other sheet protected and unprotected and it makes no difference. I get the same error: "The cell or chart you're trying to change is on a protected sheet. To make a change, unprotect the sheet. You might be requested to enter a password."

Any help would be much appreciated.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
One more thing, when I run the second piece of code, the one that fails, when the sheet is unprotected it works perfectly and even protects the sheet when it is done. It then fails on further changes because the sheet is protected.
 
Upvote 0
One more thing, when I run the second piece of code, the one that fails, when the sheet is unprotected it works perfectly and even protects the sheet when it is done. It then fails on further changes because the sheet is protected.

Hi,
when you say "it then fails on further changes" it just sounds like you need to unprotect in other places ... so you probably need to check all the places in your code where changes are being made.
 
Upvote 0
If you are using the same password for all sheets, then I suggest that you use "UserInterFaceOnly" option when protecting through VBA so you don't have to worry about unprotect & protect the sheet every time you run a macro ... Unprotect all sheets then use the below code to protect the sheets again but using "UserInterFaceOnly" this time so you don't need to unprotect the sheet to alter the sheet through VBA

Code:
Private Sub Password()

Dim ws As Worksheet

For Each ws In Worksheets
    ws.Protect Password:="ex03740", UserInterFaceOnly:=True
Next ws

End Sub
 
Upvote 0
The code runs every time cell U42 is changed. What I meant is that if the sheet containing cell U42 is unprotected the code will run once, and then protect itself at the end of the code and fail to run a second time. It is like it is skipping over the unprotect code.
 
Upvote 0
mse330, thanks for the suggestion, but it still doesn't work. For whatever reasons this piece of code:
Code:
If Target.Address = "$U$42" Then    'hides or unhides the rows for battery dischrage test
    ActiveSheet.Unprotect Password:="ex03740"
    If Target.Value = "Yes" Then
            Sheets("Info & Readings").Range("A48", "A50").EntireRow.Hidden = False
            Sheets("Discharge Test").Visible = True
        Else
            Sheets("Info & Readings").Range("A48", "A50").EntireRow.Hidden = True
            Sheets("Discharge Test").Visible = False
        End If
        ActiveSheet.Protect Password:="ex03740"
    End If
only runs when the "Info & Readings" Sheet is unprotected. It seems to make no difference to how the code runs whether or not "Discharge Test" is protected. When "Info & Readings" is unprotected the code will run if "Discharge Test" is protected.
 
Upvote 0
What do you mean "it fails to run a second time"? Fails how? What is the error?
 
Upvote 0
mse330, thanks for the suggestion, but it still doesn't work. For whatever reasons this piece of code:
Code:
If Target.Address = "$U$42" Then    'hides or unhides the rows for battery dischrage test
    ActiveSheet.Unprotect Password:="ex03740"
    If Target.Value = "Yes" Then
            Sheets("Info & Readings").Range("A48", "A50").EntireRow.Hidden = False
            Sheets("Discharge Test").Visible = True
        Else
            Sheets("Info & Readings").Range("A48", "A50").EntireRow.Hidden = True
            Sheets("Discharge Test").Visible = False
        End If
        ActiveSheet.Protect Password:="ex03740"
    End If
only runs when the "Info & Readings" Sheet is unprotected. It seems to make no difference to how the code runs whether or not "Discharge Test" is protected. When "Info & Readings" is unprotected the code will run if "Discharge Test" is protected.

Why don't just add another line to unprotect the other sheet (or all sheets) when a change is triggered then re-protect all sheets again ?
 
Upvote 0
Why don't just add another line to unprotect the other sheet (or all sheets) when a change is triggered then re-protect all sheets again ?

The issue isn't with the protection, it is that it is not running the unprotect line of code. for some reason it is skipping over it. And I cannot figure out why it is skipping over it.
 
Upvote 0
Have you set a breakpoint on line one of this code?
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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