在Excel中按“删除”键可以刷新自动date和时间

我使用一个简单的代码来自动inputdate和时间在Excel工作表中的两个单独的单元格,但是,如果我在单元格中input一个新的值,或者只是按下“删除”键,它们会自动更改。 以下是我正在使用的代码:

Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column <> 5 Then Exit Sub Application.EnableEvents = False Target.Offset(0, -2).Value = Date Application.EnableEvents = True If Target.Column <> 5 Then Exit Sub Application.EnableEvents = False Target.Offset(0, -1).Value = Time Application.EnableEvents = True End Sub 

我需要date和时间保持静态,直到我从他们各自的单元格中删除它们。 我怎样才能做到这一点?

这将保存input后的date/时间:

 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column <> 5 Then Exit Sub Application.EnableEvents = False If Target.Offset(0, -2).Value = "" And Target.Offset(0, -2).Value = "" Then Target.Offset(0, -2).Value = Date Target.Offset(0, -1).Value = Time End If Application.EnableEvents = True End Sub 

编辑#1:

该版本将允许您设置和清除列E中的多个单元格:

 Private Sub Worksheet_Change(ByVal Target As Range) Dim r As Range, i1 As Long, i2 As Long If Target.Column <> 5 Then Exit Sub With ActiveSheet.UsedRange i2 = .Rows.Count + .Row - 1 i1 = .Row End With Application.EnableEvents = False For Each r In Intersect(Target, Range("E" & i1 & ":E" & i2)) If r.Offset(0, -2).Value = "" And r.Offset(0, -1).Value = "" And r.Value <> "" Then r.Offset(0, -2).Value = Date r.Offset(0, -1).Value = Time End If Next r Application.EnableEvents = True End Sub 

清除已经为空的单元不会导致时间/datelogging。

浏览你的代码:

  Private Sub Worksheet_Change(ByVal Target As Range) If Target.Column <> 5 Then Exit Sub 

“如果目标列不是5则退出子程序”这很酷。

  Application.EnableEvents = False 

将其翻转为false将确保此代码在该值设置为true之前不会再次运行。 Worksheet_Change需要enableevents 。 所以现在如果改变的单元格在列E那么Worksheet_Change将不会再次执行。 这是有道理的,以防止无限循环发生,因为单元格通过这个代码进行更改。

  Target.Offset(0, -2).Value = Date 

将从目标单元格返回两列的单元格设置为当前date。

  Application.EnableEvents = True 

重新设置enableEvents 。 这很好,因为你可能不想离开这个。

  If Target.Column <> 5 Then Exit Sub 

为什么我们再检查一次? Target.Column自上次以来没有改变,如果它已经<>5那么我们就不会在这里testing它。 这条线是多余的。

  Application.EnableEvents = False 

好吧,我们只是把这个打开了,但是现在我们又把这个关掉了。 只要把它关掉。

  Target.Offset(0, -1).Value = Time 

将目标单元格左侧的值1列设置为当前时间。 Coolios。

  Application.EnableEvents = True 

打开enableEvents 。 这在这里是有道理的。

 End Sub 

重写这个删除冗余切换和superflous target.Column检查:

 Private Sub Worksheet_Change(ByVal Target As Range) 'make sure this is column 5 that was changed. Like if anything changed in ' column 5, then run the rest of this. If Target.Column <> 5 Then Exit Sub 'Make sure we don't infinite loop if we accidently trigger a change to ' column 5 in this code. Application.EnableEvents = False ' Set two cells to the left to the current date ' and one cell to the left to the current time Target.Offset(0, -2).Value = Date Target.Offset(0, -1).Value = Time 'turn events back on. Application.EnableEvents = True End Sub 

所以..每当你在第5列做出改变,date和时间都会改变。 如果你想这样做只会改变一次行的date和时间。 然后检查是否已经为该行设置了date和时间:

 Private Sub Worksheet_Change(ByVal Target As Range) 'make sure this is column 5 that was changed. Like if anything changed in ' column 5, then run the rest of this. If Target.Column <> 5 Then Exit Sub 'Check to see if the date and time are already set for this row: ' If they are, then exit subroutine. If target.offset(0,-2).value <> "" OR target.offset(0,-1).value <> "" Then Exit Sub 'Make sure we don't infinite loop if we accidently trigger a change to ' column 5 in this code. Application.EnableEvents = False ' Set two cells to the left to the current date ' and one cell to the left to the current time Target.Offset(0, -2).Value = Date Target.Offset(0, -1).Value = Time 'turn events back on. Application.EnableEvents = True End Sub