Multiple "Worksheet_Change" routines

seventy9mph

Board Regular
Joined
Apr 16, 2009
Messages
72
here is my code, and I need help either combining the two Worksheet_Change routines or getting another way to do this. Thanks in advance



Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$I$2" Then Exit Sub
If Target = "GB" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$F$6:$F$8").ClearContents
Range("$F$4").Locked = False
Range("$F$5:$F$8").Locked = True
Range("$G$6:$G$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
If Target.Address <> "$I$2" Then Exit Sub
If Target = "VC" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$F$4").Locked = False
Range("$F$6").Locked = False
Range("$F$7").Locked = False
Range("$F$8").Locked = False
Range("$F$5").Locked = True
Range("$G$6:$G$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
If Target.Address <> "$I$2" Then Exit Sub
If Target = "DONE" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$F$4").ClearContents
Range("$F$6:$F$8").ClearContents
Range("$F$4:$G$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
End If
End If
End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "$P$2" Then Exit Sub
If Target = "GB" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$M$6:$M$8").ClearContents
Range("$M$4").Locked = False
Range("$M$5:$M$8").Locked = True
Range("$N$6:$N$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
If Target.Address <> "$P$2" Then Exit Sub
If Target = "VC" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$M$4").Locked = False
Range("$M$6").Locked = False
Range("$M$7").Locked = False
Range("$M$8").Locked = False
Range("$M$5").Locked = True
Range("$N$6:$N$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
If Target.Address <> "$P$2" Then Exit Sub
If Target = "DONE" Then
ActiveSheet.Unprotect ("Branches080704")
Application.EnableEvents = False
Range("$M$4").ClearContents
Range("$M$6:$M$8").ClearContents
Range("$M$4:$N$8").Locked = True
Application.EnableEvents = True
ActiveSheet.Protect ("Branches080704")
Else
End If
End If
End If
End Sub
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim intColumn As Integer
    
    If Intersect(Target, Range("I2"), Range("P2")) Is Nothing Then
        Exit Sub
    End If
                
    Select Case Target.Address
        Case "$I$2"
            intColumn = 5
        Case "$P$2"
            intColumn = 13
    End Select

    Application.EnableEvents = False
    ActiveSheet.Unprotect ("Branches080704")
    
    If Target = "GB" Then
        Cells(intColumn, 6).Resize(3, 1).ClearContents
        Cells(intColumn, 4).Locked = False
        Cells(intColumn, 5).Resize(4, 1).Locked = True
        Cells(intColumn + 1, 6).Resize(3, 1).Locked = True
    ElseIf Target = "VC" Then
        Cells(intColumn, 4).Locked = False
        Cells(intColumn, 6).Resize(3, 1).Locked = False
        Cells(intColumn, 5).Locked = True
        Cells(intColumn + 1, 6).Resize(3, 1).Locked = True
    ElseIf Target = "DONE" Then
        Cells(intColumn, 4).ClearContents
        Cells(intColumn, 6).Resize(3, 1).ClearContents
        Cells(intColumn, 4).Resize(5, 2).Locked = True
    End If
    
    ActiveSheet.Protect ("Branches080704")
    Application.EnableEvents = True

End Sub
Since you're doing the same thing, just with different columns, I simplified it.
 
Upvote 0

Forum statistics

Threads
1,214,810
Messages
6,121,690
Members
449,048
Latest member
81jamesacct

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