Sheet protection getting "re-activated" automatically in the middle of my macro

PL dub

New Member
Joined
Oct 17, 2015
Messages
39
Hello,

In my code below, the sheet protection is re-activated automatically after the line ".ClearContents". To Avoid this situation, I must unprotect the sheet again between the lines ".ClearContents" and ".Locked = True"

Any idea why this is happening and how I can avoid unprotecting my sheet again the the middle of my code ?

Another less important question, My screenUpdating = False doesn`t seems to work, any suggestion ?

I dont know if it has any influence, but the macro is activated on a change of a specific cell.

VBA Code:
With Application
     .ScreenUpdating = False
     .Calculation = xlCalculationManual
End With

With Worksheets("Sheet1")
     .Unprotect Password:="2020"
     .DisplayPageBreaks = False
End With

Dim FileType As String
Dim ws1 As Worksheet
Dim rngA As Range
Dim rngB As Range
Set ws1 = Worksheets("Sheet1")

With ws1
    Set rngA = .Range("M30:M35, K42:M43, K44")
    Set rngB = .Range("I37:K39, L47:L48")
End with

FileType = ws1.Range("E10").Value2

              
           If FileType = "AAA" Then                     
                With rngB
                     .Interior.Pattern = (14)
                    .ClearContents
                    .Locked = True
                End With

               With rngA 
                   .Interior.Pattern = (-4142)
                   .Locked = False
               End with
           End If

With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With

With Worksheets("Sheet1")
.DisplayPageBreaks = True
.Protect Password:="2020"
End With
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Which cell(s) need to trigger the code?
 
Upvote 0
Which cell(s) need to trigger the code?
Here is my code updated with the trigger. Code reside in Sheet1(Sheet1). It is not in a Module.

Inside the "Private Sub Worksheet_Change(ByVal Target As Range)".

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If Not Application.Intersect(Target, Range("E10")) _
           Is Nothing Then

With Application
     .ScreenUpdating = False
     .Calculation = xlCalculationManual
End With

With Worksheets("Sheet1")
     .Unprotect Password:="2020"
     .DisplayPageBreaks = False
End With

Dim FileType As String
Dim ws1 As Worksheet
Dim rngA As Range
Dim rngB As Range
Set ws1 = Worksheets("Sheet1")

With ws1
    Set rngA = .Range("M30:M35, K42:M43, K44")
    Set rngB = .Range("I37:K39, L47:L48")
End with

FileType = ws1.Range("E10").Value2

              
           If FileType = "AAA" Then                     
                With rngB
                     .Interior.Pattern = (14)
                    .ClearContents
                    .Locked = True
                End With

               With rngA 
                   .Interior.Pattern = (-4142)
                   .Locked = False
               End with
           End If

With Application
.ScreenUpdating = True
.Calculation = xlCalculationAutomatic
End With

With Worksheets("Sheet1")
.DisplayPageBreaks = True
.Protect Password:="2020"
End With

End if
End sub
 
Upvote 0
I can see no reason why the sheet should get re-protected part way through that code.
Try putting a break point on this line
VBA Code:
If Not Application.Intersect(Target, Range("E10")) _
           Is Nothing Then
then change E10 & step through the code using F8, that should allow you to see when the sheet is getting protected.
 
Upvote 0
I'll try what you suggest.

Yesterday I changed the way I "Set" the Range as below. With this change, the sheet does not get re-protected by itself. :unsure:

VBA Code:
Dim rngA As Range
Dim rngB As Range


    Set rngA = Worksheets("Sheet1").Range("M30:M35, K42:M43, K44")
    Set rngB = Worksheets("Sheet1").Range("I37:K39, L47:L48")
 
Upvote 0
That shouldn't make any difference.
 
Upvote 0

Forum statistics

Threads
1,213,530
Messages
6,114,163
Members
448,554
Latest member
Gleisner2

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