繁体   English   中英

通过条目在Excel中创建自动日期戳

[英]Create an automatic date stamp in excel from an entry

我试图在A列中创建条目时在B列中发生日期戳事件。现在我可以在VBA中做到这一点,没有问题,我遇到的麻烦是还有一个条目最终会进入例如说D列,那么E列也需要日期戳。 这可能吗。 这是我到目前为止使用的代码示例。

如果目标Cell.Column <= 3,则如果目标Cells(Cell.Row,1)<>“”然后目标单元格(Cell.Row,2)=现在结束,如果目标下一个单元格结束子

如果您可以接受用户输入的每个奇数列,并且时间戳记在偶数列中(即,您可以在列A中键入,而时间戳记将在列B中。您可以键入列C和时间戳)将进入D列等),则可以使用以下代码:

Private Sub Worksheet_Change(ByVal Target As Range)

    'Only write a timestamp of an odd column changes (because the timestamps go in the even columns)
    If Target.Column Mod 2 > 0 Then

        'Get the first part of the address, to get the actual column being changed
        Dim columnAddress As String
        columnAddress = Target.Address

        If InStr(columnAddress, ":") > 0 Then
            columnAddress = Left(columnAddress, InStr(columnAddress, ":") - 1)
        End If

    'This will cause the TimeStamp to be undeletable (kind of like na Audit).
    'If you want the timestamp to disappear when you clear the column, uncomment the next few lines:

    '        If Not ActiveSheet.Range(columnAddress).Formula = "" Then

            ''Write the timestamp for the previous column
            ActiveSheet.Range(columnAddress).Offset(0, 1).Formula = Now

    '        Else
    '            ActiveSheet.Range(columnAddress).Offset(0, 1).Formula = ""
    '        End If
    End If


End Sub

您可以隐藏不需要时间戳的列。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM