提示:其它语言是由 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中的相邻单元格将立即填入当前日期。 看截图:


相关文章:


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,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.
    Abbey · 7 days ago
    Hello, Can I set the date and the time to populate?
  • To post as a guest, your comment is unpublished.
    Joe · 3 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 · 6 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 · 6 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 · 7 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 · 7 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
  • To post as a guest, your comment is unpublished.
    sri laqshya · 1 years ago
    Hi,

    Can anyone one suggest a code for when I input number in column A and Column B and in Column C I kept a formula like Column A + Column B. Now I need a vba code that can give time and date in Column D whenever Column C changes not when inserting numbers in Column A and B.
  • To post as a guest, your comment is unpublished.
    Sarah · 1 years ago
    Are you able to use this function twice on the same sheet? i.e. if I would like to make entries in column B to time stamp column A AND put entries in column D to timestamp column C. Thanks!
    • To post as a guest, your comment is unpublished.
      John · 1 years ago
      I got this to work by doing the following:


      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


      On Error Resume Next
      If (Target.Count = 1) Then
      If (Not Application.Intersect(Target, Me.Range("D:D")) Is Nothing) Then _
      Target.Offset(0, -1) = Date
      Application.EnableEvents = False
      Set xRg = Application.Intersect(Target.Dependents, Me.Range("D:D"))
      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
  • To post as a guest, your comment is unpublished.
    Michael · 1 years ago
    Thanks it works fine, but when it comes to close and save I'm getting a error that the feature "VB project" cannot be saved in a macro-free workbook. Please advise
    • To post as a guest, your comment is unpublished.
      Jay · 1 years ago
      You just need to "save as" a excel macro-enabled workbook....
  • To post as a guest, your comment is unpublished.
    Michael · 1 years ago
    Thanks it works fine, but when it comes to close and save I'm getting a error that the feature "VB project" cannot be saved in a macro-free workbook. Please advise
  • To post as a guest, your comment is unpublished.
    Tammy · 2 years ago
    Can this be accomplished on a cell that contains a formula?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Tammy,
      The code has been updated. It can be accomplished on a cell that contains a formula now. Thank you for your comment.
  • To post as a guest, your comment is unpublished.
    leah · 2 years ago
    I have a set of excel worksheets titled monday, tuesday, wednesday, etc. I need to put the beginning date on Monday in cell a1, and have it fill in the subsequent dates for tuesday through Friday in cell a1 on each of those sheets. I am not code literate at all, so I just need to know what the simple formula I can put in there is. :) thank you!
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear leah,
      You just have to use the formula =monday!A1+1 on sheet tuesday, =tuesday!A1+1 on sheet wednesday and so on...
  • To post as a guest, your comment is unpublished.
    Jishnu · 2 years ago
    Thank You. This helped a lot.
    But when I am deleting a row or adding a row this gives an Run-time error 13 Type mismatch.

    How to tackle this issue.

    Expecting a revert ASAP.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Jishnu,
      The problem you mentioned does not appear in my case. Would you please provide your Office version?