提示:其他語言是Google翻譯的。 你可以訪問 English 版本。
登入
x
or
x
x
註冊
x

or

如何在Excel中更新相鄰單元格時自動填充單元格中的日期?

有時,當更新特定列中的單元格時,可能需要標記關於更新的最新日期。 本文將推荐一種VBA方法來解決這個問題。 當單元格更新時,相鄰單元格將立即自動填入當前日期。

使用VBA代碼更新相鄰單元格時,自動填充單元格中的當前日期


將多個工作表/工作簿合併到一個工作表/工作簿中:

將多個工作表或工作簿合併到一個工作表或工作簿中可能是您​​日常工作中的一項重大任務。 但是,如果你有 Kutools for Excel,其強大的實用性 - 結合 可以幫助您將多個工作表,工作簿快速合併到一個工作表或工作簿中

Kutools for Excel:比200方便的Excel加載項,可以在60天免費試用。 立即下載免費試用版!


使用VBA代碼更新相鄰單元格時,自動填充單元格中的當前日期


假設您需要更新的數據位於列B中,並且當列B中的單元格更新時,當前日期將填充到列A的相鄰單元格中。請參見屏幕截圖:

您可以運行以下VBA代碼來解決此問題。

1。 右鍵單擊工作表選項卡,根據相鄰更新的單元格自動填充日期,然後單擊 查看代碼 從右鍵菜單。

2。 在Microsoft Visual Basic for Applications窗口中,請將以下VBA代碼複製並粘貼到代碼窗口中。

VBA代碼:在更新相鄰單元格時,自動在單元格中填充當前日期

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
'Updated by Extendoffice 2017/10/12
    Dim xRg As Range, xCell As Range
    On Error Resume Next
    If (Target.Count = 1) Then
        If (Not Application.Intersect(Target, Me.Range("B:B")) Is Nothing) Then _
            Target.Offset(0, -1) = Date
        Application.EnableEvents = False
        Set xRg = Application.Intersect(Target.Dependents, Me.Range("B:B"))
        If (Not xRg Is Nothing) Then
            For Each xCell In xRg
                xCell.Offset(0, -1) = Date
            Next
        End If
        Application.EnableEvents = True
    End If
End Sub

筆記:

1。 在代碼中,B:B表示更新的數據位於B列中。

2。 -1表示當前日期將填充在B列的左側一列中。如果要將當前日期填充到列C中,請將-1更改為1。

3。 按 其他 + Q 鍵同時關閉 Microsoft Visual Basic for Applications 窗口。

從現在開始,當更新列B中的單元格時,列A中的相鄰單元格將立即填入當前日期。 看截圖:


相關文章:



推薦的Excel生產力工具

Kutools for Excel幫助您提前完成工作,並從人群中脫穎而出

  • 超過300強大的高級功能,專為1500工作場景設計,通過70%提高生產力,讓您有更多時間照顧家庭和享受生活。
  • 不再需要記憶公式和VBA代碼,從現在起讓你的大腦休息一下。
  • 成為3分鐘的Excel專家,複雜和重複的操作可以在幾秒鐘內完成,
  • 每天減少成千上萬的鍵盤和鼠標操作,現在告別職業病。
  • 110,000高效人才和300 +世界知名公司的選擇。
  • 60-day full功能免費試用。 60天退款保證。 2多年的免費升級和支持。

將選項卡式瀏覽和編輯帶到Microsoft Office,遠比瀏覽器的選項卡強大

  • Office選項卡專為Word,Excel,PowerPoint和其他Office應用程序設計:Publisher,Access,Visio和Project。
  • 在同一窗口的新選項卡中打開並創建多個文檔,而不是在新窗口中。
  • 通過50%提高您的工作效率,每天為您減少數百次鼠標點擊!
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Joe · 1 months ago
    Hello, I need some help. I am trying to populate the current date into column A if any cells B through N are modified. The offset is throwing me off. Can this code be written to auto populate the date in Column A specifically?
  • To post as a guest, your comment is unpublished.
    Gwen · 4 months ago
    Hi. I'm trying to use the "Auto Populate Current Date In Cell When Adjacent Cell Is Updated With VBA Code". This works on Sheet 1, but I have 11 other sheets I need the code to work on. Please advise. I'm not VBA saavy at all so I appreciate any help I can get!

    Thanks.
    • To post as a guest, your comment is unpublished.
      crystal · 4 months ago
      Hi Gwen,
      Please repeat the steps to copy the code to other sheets. It may be tedious, but it works.
  • To post as a guest, your comment is unpublished.
    Tracey · 5 months ago
    I used this code to auto populate a column and now wish to auto populate more columns based on date entered into column H. In other words, once a date is entered can I auto populate columns to add date 90, 60, and 30 days out?
    • To post as a guest, your comment is unpublished.
      crystal · 5 months ago
      Hi Tracey,
      The following VBA code can help you solve the probem. Please have a try.

      Private Sub Worksheet_Change(ByVal Target As Excel.Range)
      Dim xRg As Range, xCell As Range
      Dim xRgAddress As String
      xRgAddress = "H:H"
      On Error Resume Next
      If (Target.count = 1) Then
      If (Not Application.Intersect(Target, Me.Range(xRgAddress)) Is Nothing) Then
      Target.Offset(0, 1) = Date + 90
      Target.Offset(0, 2) = Date + 60
      Target.Offset(0, 3) = Date + 30
      End If
      Application.EnableEvents = False
      Set xRg = Application.Intersect(Target.Dependents, Me.Range(xRgAddress))
      If (Not xRg Is Nothing) Then
      For Each xCell In xRg
      xCell.Offset(0, 1) = Date + 90
      xCell.Offset(0, 2) = Date + 60
      xCell.Offset(0, 3) = Date + 30
      Next
      End If
      Application.EnableEvents = True
      End If
      End Sub
  • To post as a guest, your comment is unpublished.
    Jen · 1 years ago
    Nevermind...I tried John's response below and it works - thank you!
  • To post as a guest, your comment is unpublished.
    Jen · 1 years ago
    HI - new to VBA - I want to loop the code - VBA code: auto populate current date in a cell when the adjacent cell is updated so that after a cell is updated with a date, then move on to say "J:J" and update K with date and then do 2 more times. Not sure if it is a loop or for? Thank you