如果单元格包含某些文本,则locking相邻单元格

我试图修改以下代码以locking相邻的单元格时,列M包含“否”

例如,如果M12包含“否”,我想locking“V12:AG12,AI12:AT12”

Private Sub Worksheet_Change(ByVal Target As Range) Dim rng1 As Range Dim c As Range Set rng1 = Intersect(Target, Range("M:M")) If rng1 Is Nothing Then Exit Sub ActiveSheet.Unprotect Password:="Password" With Application .ScreenUpdating = False .EnableEvents = False End With For Each c In rng1 Select Case LCase(c.Value) Case Is = "YES" ActiveSheet.Unprotect Password:="Password" Cells(c.Row, 13).Resize(1, 12).Locked = False Range(Cells(c.Row, "V"), Cells(c.Row, "AG")).Locked = False Range(Cells(c.Row, "AI"), Cells(c.Row, "AT")).Locked = False ActiveSheet.Protect Password:="Password" Case Is = "NO" ActiveSheet.Unprotect Password:="Password" Range(Cells(c.Row, "V"), Cells(c.Row, "AG")).Locked = True Range(Cells(c.Row, "AI"), Cells(c.Row, "AT")).Locked = True ActiveSheet.Protect Password:="Password" Case Else ActiveSheet.Unprotect Password:="Password" MsgBox "Please only input YES or NO in this column", vbCritical + vbOKOnly ActiveSheet.Protect Password:="Password" Exit Sub End Select Next c With Application .ScreenUpdating = True .EnableEvents = True End With ActiveSheet.Protect Password:="Password" End Sub 

然而我很困惑,我会在上面的代码中插入条件“否”,以及如何select范围到“locking”

附上一张照片来展示我正在努力实现的目标。 在这里输入图像说明

谢谢

这应该是做你想做的事情:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim rng1 As Range Dim c As Range Set rng1 = Intersect(Target, Me.Range("M:M")) If rng1 Is Nothing Then Exit Sub With Application .ScreenUpdating = False .EnableEvents = False End With Me.Unprotect Password:="Password" For Each c In rng1 Select Case LCase(c.Value) Case Is = "yes" Me.Cells(c.Row, 13).Resize(1, 12).Locked = False Me.Range(Me.Cells(c.Row, "V"), Me.Cells(c.Row, "AG")).Locked = False Me.Range(Me.Cells(c.Row, "AI"), Me.Cells(c.Row, "AT")).Locked = False Case Is = "no" Me.Range(Me.Cells(c.Row, "V"), Me.Cells(c.Row, "AG")).Locked = True Me.Range(Me.Cells(c.Row, "AI"), Me.Cells(c.Row, "AT")).Locked = True Case Else MsgBox "Please only input YES or NO in this column", vbCritical + vbOKOnly Exit Sub End Select Next c Me.Protect Password:="Password" With Application .ScreenUpdating = True .EnableEvents = True End With End Sub 

请注意,我还包含了一些行来解锁将被“locking”的单元格,以便在您将值更改为“是”时解锁它们,