如果右侧2个单元格(同一行,C列)中有任何内容,如何使范围(“ A1:A10”)中的任何单元格都无法编辑(无法更改),并使该单元格再次可编辑一次同一行列C中的单元格为空。 我尝试了下面的代码,但没有任何效果,即使在C列中包含内容,我仍然可以编辑A列中的单元格。理想情况下,我希望在不保护工作表的情况下完成它。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("C1:C10")) Is Nothing Then
If Target.Value <> "" Then
Target.Offset(0, -2).Cells.Locked = True
ActiveSheet.Protect Contents:=True
End If
Application.EnableEvents = True
End If
End Sub
谢谢, 杰伊
答案 0 :(得分:0)
您正在检查Target.Address
是否为空字符串。为什么?那不是Target.Value
吗?
最重要的是,怎么了?您是否使用断点调试了代码?你看见什么了? ...如果我的答案不能满足您的需求,请编辑您的问题并添加所需的信息。
答案 1 :(得分:0)
Option Explicit
'*** Note: I need to reside in the Sheet Module for the
' sheet I am working on!
'*** Note: Make sure the Locked flags for A1:A10 and
' C1:C10 are cleared before implementing.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Application.EnableEvents = False 'Turn off Events to prevent loop!
If Not Intersect(Target, Range("C1:C10")) Is Nothing Then
ActiveSheet.Unprotect 'You need to unprotect before proceeding
For Each rng In Target
If rng <> "" Then 'Checking for Target.Address will always have a value!
rng.Offset(0, -2).Locked = True
Else
rng.Offset(0, -2).Locked = False
End If
Next rng
ActiveSheet.Protect Contents:=True 'Turn Protection back on
'*** If you have other protected elements DrawingObjects and/or Scenarios
' you need to include in line above.
End If
Application.EnableEvents = True 'Re-enable Events
End Sub 'Worksheet_Change
HTH