使用命令按钮,如何让命令按钮锁定其关联的单元格?
这是我的代码。
Private Sub TimeGo_Click()
With Me.TimeGo
If .Caption = "Start Time" Then
Rw = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(Rw, 1).Value = Format(Now, "hh:mm:ss")
.Caption = "End Time"
Else: .Caption = "Start Time"
Cells(Rw, 2).Value = Format(Now, "hh:mm:ss")
Cells(Rw, 3).Formula = "=mod(RC[-1]-RC[-2],1)"
End If
End With
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:C200")) Is Nothing Then 'set your range here
ActiveSheet.Unprotect Password:="mypassword"
Target.Locked = True
ActiveSheet.Protect Password:="mypassword"
End If
End Sub
答案 0 :(得分:0)
您的问题有点令人困惑,但如果我理解正确,那么您希望TimeGo_Click调用Worksheet_Change subs来锁定单元格。
如果是这样,我建议一个新的子包含可以从Worksheet_Change和TimeGo_Click调用的代码。然后你可以从两个sub中调用它作为illusatrated(为了简洁,我没有包括TimeGo_Click sub,而且我不确定你想要调用的子在哪里):
Private Sub Worksheet_Change(ByVal Target As Range)
Call LockSingeCell(Target)
End Sub
Private Sub LockSingeCell(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("A1:C200")) Is Nothing Then 'set your range here
ActiveSheet.Unprotect Password:="mypassword"
Target.Locked = True
ActiveSheet.Protect Password:="mypassword"
End If
End Sub