当I列包含“NO”
时,我正在尝试调整以下代码来锁定相邻的单元格例如,如果M12包含“NO”,我想锁定“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
但是我很困惑我会在上面的代码中插入条件“NO”以及如何选择“锁定”的范围
由于
答案 0 :(得分:0)
这应该是你想要的:
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
请注意,我还包括用于解锁将被" NO"如果您将值更改为"是"
,则可以解锁它们