简体   繁体   English

更新另一个单元格时自动将日期/时间添加到单元格,并在该单元格为空时将其清除

[英]Automatically adding the date/time to a cell when another cell is updated AND clearing it when this cell is empty

I'm creating excel file for company reports.我正在为公司报告创建 excel 文件。 I've borrowed script from this MS Support thread - LINK我从这个 MS 支持线程中借用了脚本 - LINK
It looks like that:它看起来像这样:

Sub Worksheet_Change(ByVal Target As Range)

 If Not Intersect(Target, Range("A1")) Is Nothing Or _
    Not Intersect(Target, Range("B1")) Is Nothing Or _
    Not Intersect(Target, Range("C1")) Is Nothing Or _
    Not Intersect(Target, Range("D1")) Is Nothing Or _
    Not Intersect(Target, Range("E1")) Is Nothing Then

    Target.Offset(1, 0) = Now
 End If

End Sub

It works fine but now i want target cells to be cleared after cleaing main cells.它工作正常,但现在我希望在清除主单元格后清除目标单元格。
Now when for example i erase A1, the time in A2 stays there.现在,例如,当我擦除 A1 时,A2 中的时间会停留在那里。
I'm quite new in Excel and hope for some help:)我是 Excel 的新手,希望能得到一些帮助:)
Have a good day!祝你有美好的一天!

............................... ...................................

You are looking for an IsEmpty check in your code:您正在寻找代码中的IsEmpty检查:

If IsEmpty(Target.Value) Then Target.Offset(1, 0).ClearContents Else Target.Offset(1, 0) = Now

As a whole:整体而言:

Sub Worksheet_Change(ByVal Target As Range)

 If Not Intersect(Target, Range("A1:E1")) Is Nothing Then

    If IsEmpty(Target.Value) Then Target.Offset(1, 0).ClearContents Else Target.Offset(1, 0) = Now

 End If

End Sub

暂无
暂无

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

相关问题 更新/更改单元格时自动输入日期和时间 - Automatically enter date & time as cell is updated/changed 当具有列表验证的单元格被更新(用户从列表中选择其他值)时,另一个单元格将自动更新 - When a cell with list validation gets updated (user selects different value from list), another cell automatically updates 当填充另一个单元格时,自动将值设置为Excel中的空单元格 - Automatically set value to empty cells in Excel when another cell is populated Excel 单元格变化时自动记录日期和时间 VBA - Excel Record Date And Time Automatically When Cell Changes VBA 当日期在 excel 中的另一个单元格上更新时,我想增加单元格上的数字 - i want to increment the number on a cell when date is updated on another cell in excel Excel 在单元格更新时创建日期戳 - Excel create date stamp when an cell is updated 当我将一个单元格链接到单独页面上的另一个空单元格时,1900 年的年份和日期不断出现 - The year and date of 1900 keeps appearing when I link one cell to another empty cell on a separate page Excel-第一次更改另一个单元格时用日期时间更新列 - Excel - Update Column with Date Time when Another Cell is First Changed 当另一个单元格中有值输入时,Excel在单元格中输入日期时间 - Excel entering the date time into a cell when another cell has a value entered into it 当数据输入到同一行的另一个单元格时,Excel 公式将日期/时间放在单元格中 - Excel Formula which places date/time in cell when data is entered in another cell in the same row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM