Apply workbook_change to multiple ranges

sdkorin

New Member
Joined
Feb 1, 2018
Messages
22
Office Version
  1. 2016
Platform
  1. Windows
I am trying to lock a row based on the value of the first cell. I've got my VBA working fine for one row, but I need to apply this to multiple rows, all independently.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A13") = "CES" Or Range("A13") = "CES Start" Or Range("A13") = "CES End" Then
        Range("D13:CA13").Locked = False
    ElseIf Range("A13") = "" Then
        Range("D13:CA13").Locked = True
    End If
End Sub

This works for my single row. I just need this to work exactly the same, but independently for each row, for rows 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, and 37.

This is obviously now working, but this gives you the idea of what I'm looking for.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A13") = "CES" Or Range("A13") = "CES Start" Or Range("A13") = "CES End" Then
        Range("D13:CA13").Locked = False
    ElseIf Range("A13") = "" Then
        Range("D13:CA13").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A15") = "CES" Or Range("A15") = "CES Start" Or Range("A15") = "CES End" Then
        Range("D15:CA15").Locked = False
    ElseIf Range("A15") = "" Then
        Range("D15:CA15").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A17") = "CES" Or Range("A17") = "CES Start" Or Range("A17") = "CES End" Then
        Range("D17:CA17").Locked = False
    ElseIf Range("A17") = "" Then
        Range("D17:CA17").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A19") = "CES" Or Range("A19") = "CES Start" Or Range("A19") = "CES End" Then
        Range("D19:CA19").Locked = False
    ElseIf Range("A19") = "" Then
        Range("D19:CA19").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A21") = "CES" Or Range("A21") = "CES Start" Or Range("A21") = "CES End" Then
        Range("D21:CA21").Locked = False
    ElseIf Range("A21") = "" Then
        Range("D21:CA21").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A23") = "CES" Or Range("A23") = "CES Start" Or Range("A23") = "CES End" Then
        Range("D23:CA23").Locked = False
    ElseIf Range("A23") = "" Then
        Range("D23:CA23").Locked = True
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A25") = "CES" Or Range("A25") = "CES Start" Or Range("A25") = "CES End" Then
        Range("D25:CA25").Locked = False
    ElseIf Range("A25") = "" Then
        Range("D25:CA25").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A27") = "CES" Or Range("A27") = "CES Start" Or Range("A27") = "CES End" Then
        Range("D27:CA27").Locked = False
    ElseIf Range("A27") = "" Then
        Range("D21:CA27").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A29") = "CES" Or Range("A29") = "CES Start" Or Range("A29") = "CES End" Then
        Range("D29:CA29").Locked = False
    ElseIf Range("A29") = "" Then
        Range("D29:CA29").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A31") = "CES" Or Range("A31") = "CES Start" Or Range("A31") = "CES End" Then
        Range("D31:CA31").Locked = False
    ElseIf Range("A31") = "" Then
        Range("D31:CA31").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A33") = "CES" Or Range("A33") = "CES Start" Or Range("A33") = "CES End" Then
        Range("D33:CA33").Locked = False
    ElseIf Range("A33") = "" Then
        Range("D33:CA33").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A35") = "CES" Or Range("A35") = "CES Start" Or Range("A35") = "CES End" Then
        Range("D35:CA35").Locked = False
    ElseIf Range("A35") = "" Then
        Range("D35:CA35").Locked = True
    End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("A37") = "CES" Or Range("A37") = "CES Start" Or Range("A37") = "CES End" Then
        Range("D37:CA37").Locked = False
    ElseIf Range("A37") = "" Then
        Range("D37:CA37").Locked = True
    End If
End Sub

Thoughts? Thank you for the help.
 
Try like this
Code:
Intersect(Rows(Target.Row), Range("D:E,H:H,L:L,N:N,Q:R,U:U,Y:Y")).Locked = True
 
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,093
Latest member
dbomb1414

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