按下"删除"自动更新日期和时间键入Excel

时间:2016-03-16 12:25:06

标签: excel excel-vba date macros vba

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

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

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

2 个答案:

答案 0 :(得分:1)

这将保留输入后的日期/时间:

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

清除已经为空的单元格不会导致时间/日期录制。

答案 1 :(得分:0)

单步执行代码:

 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

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

    Application.EnableEvents = True

重新设置enableEvents。这很好,因为你可能不想放弃它。

    If Target.Column <> 5 Then Exit Sub

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

    Application.EnableEvents = False

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

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

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

     Application.EnableEvents = True

重新开启enableEvents。这在这里是有道理的。

End Sub

重写此项以删除冗余切换和超级目标。列检查:

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列进行更改时,日期和时间都会发生变化。如果你想要它,它只会改变一行的日期和时间。然后检查是否已为该行设置日期和时间:

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