Excel 2016 VBA 自动时间插入

Excel 2016 VBA Automatic Time Insertion

我在网上四处寻找答案,但没有找到任何关于我正在尝试做的事情的帮助。我希望在修改A列单元格时自动将时间输入到B列中的相应单元格中,C列和D列也是如此。我不希望时间更新,这是我目前遇到的问题.以下是我目前拥有的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Column = 1 Then
    Target.Offset(, 1) = Time
ElseIf Target.Column = 3 Then
    Target.Offset(, 1) = Time
End If
Application.EnableEvents = True
End Sub

此编码有效,但它更新了 B 列和 D 列中的时间,这是我不想要的。我可以添加什么来停止更新时间?

谢谢 <3

如果我对你的问题的理解正确,你只想在相邻列中没有显示时间的情况下更新时间?在那种情况下,这应该有效:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target.Column = 1 Or Target.Column = 3 Then
        If Target.Offset(, 1) = vbNullString Then Target.Offset(, 1) = Now()
    End If
    Application.EnableEvents = True
End Sub

这与 Gary's Student 的回答一致,但添加了条件并稍微简化了代码。