如何创build一个input数据的macros然后lockinginput的数据

使用命令button,我怎样才能让我的命令buttonlocking其关联的单元格?

这是我的代码。

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 

你的问题有点令人困惑,但如果我理解正确,那么你需要TimeGo_Click来调用Worksheet_Change潜艇来locking单元格。

如果是这样,我build议一个新的子包含可以从Worksheet_Change和TimeGo_Click调用的代码。 然后你会从这两个潜艇调用它作为illusatrated(我没有包括TimeGo_Click子简洁,而我不知道在哪里你会希望打电话去):

 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