GATA_Spread
New Member
- Joined
- Aug 3, 2023
- Messages
- 4
- Office Version
- 365
- Platform
- Windows
I am trying to add macros to a worksheet that will protect certain cells based on the values from other cells. There will be three different ranges that are all affected by sperate, independent cells. I've created the first macro and it works for the first range, but can't figure out how to add in for the other two ranges for it to work properly. Here's the Macro, again it works for range B1:B4 when A1 is "Yes", but I also need the same to work for Range C1:C4 when A5 is "Yes", and D1:D4 to work when A10 is "Yes".
VBA Code:
Private Sub Worksheet_Activate()
If Not ActiveSheet.ProtectContents Then
Range("A1").Locked = False
Range("B1:B4").Locked = False
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRg As Range, xRgA As Range
On Error Resume Next
Application.EnableEvents = False
Set xRg = Range("B1:B4")
Set xRgA = Range("A1")
If Intersect(Target, xRg).Address <> Target.Address _
Or xRgA = "Yes" Then
Application.EnableEvents = True
Exit Sub
ElseIf ActiveSheet.ProtectContents _
And Intersect(Target, xRg) = Target _
And xRgA.Value <> "Yes" Then
xRgA.Select
End If
Application.EnableEvents = True
End Sub