簡體   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