我正在开发一张工作表。当员工完成一行时,我或我的经理需要能够将我们的名字放在J列中,以便锁定相应的行以防止进一步编辑。有没有办法做到这一点?
答案 0 :(得分:0)
首先解锁工作表中的每个单元格。然后保护工作表。
接下来将此代码添加到Worksheet事件模块:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
If Target.Column = 4 Then 'IF Changed cell is in Column D
For Each C In Target.Cells
If C.Value = "YourName" Or C.Value = "YourManagersName" Then
ActiveSheet.Unprotect
C.EntireRow.Locked = True
ActiveSheet.Protect
End If
Next C
End If
End Sub
现在,如果在特定列中输入了两个名称之一,那么它将锁定该行。
如果您想使用密码:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim C As Range
If Target.Column = 4 Then 'IF Changed cell is in Column D
For Each C In Target.Cells
If C.Value = "YourName" Or C.Value = "YourManagersName" Then
ActiveSheet.Unprotect "Password"
C.EntireRow.Locked = True
ActiveSheet.Protect "Password"
End If
Next C
End If
End Sub
注意: 这也允许您一次锁定多行。