gpt4 book ai didi

excel - 一个单元格中的值更改时的多个时间戳

转载 作者:行者123 更新时间:2023-12-04 21:02:59 24 4
gpt4 key购买 nike

我正在尝试记录在 A 列的一个单元格中所做的多项更改。在我的示例中,我有一个用户会在 A5 列中输入日期和时间。稍后,用户可能会更改同一单元格 (A5) 中的值。这种变化最多可能发生 5 次。我如何记录从 AH 列开始的所有 5 个这些更改的日期和时间。

因此,A5 的第一次变化应记录在 AH5 栏中,A5 的第二次变化应记录在 AI5 栏中,以此类推。

我发现了多个宏,但它们每次仅在同一单元格中为更改的日期和时间加上时间戳。

最佳答案

将以下代码放入您的目标工作表模块:

Private Sub Worksheet_Change(ByVal Target As Range)
Static RecCell As Range
If Target.Address = "$A$5" Then
Select Case True
Case RecCell Is Nothing
Set RecCell = Target.Parent.Range("AH5")
RecCell.Value = Target.Value
Case RecCell.Column < Target.Parent.Range("AL5").Column
Set RecCell = RecCell.Offset(0, 1)
RecCell.Value = Target.Value
End Select
End If
End Sub

worksheet change event handler

然后是第一个 A5打开的工作簿中的更改将保存到 AH5 ,接下来的四个更改为 AI5:AL5 , 进一步的更改将被忽略。

更新

这是满足您最后要求的代码。为了使其足够灵活,我添加了一些额外的检查,这些检查禁止记录值,如果它不是日期或与先前记录的日期不同。您可以通过删除相应的 Case 轻松更改这些限制。声明行 - 请参阅我的评论。如果 e ,它还会处理所有更改的单元格。 G。复制的单元格已粘贴到多个选定的单元格。
Private Sub Worksheet_Change(ByVal Target As Range)
' Add reference: Menu - Tools - References - Microsoft Scripting Runtime
Static RecList As New Dictionary ' dictionary stores a number of column last record have been made for each row
Dim Cell As Range
For Each Cell In Target ' loop through all cells that have been changed
With Cell
Select Case True
Case .Column <> 1 ' exit if column is not A
Exit Sub
Case .Row < 5 Or .Row > 205 ' exit if row is out of target range
Exit Sub
Case Not IsDate(.Value) ' exit if changed value hasn't got a date format
Exit Sub
Case Not RecList.Exists(.Row) ' first change in this row
RecList(.Row) = Range("AH:AH").Column ' start recording from AH, proceed with value assigning
Case .Parent.Cells(.Row, RecList(.Row)) = .Value ' exit if current entered value is the same as the previous
Exit Sub
Case RecList(.Row) < Range("AL:AL").Column ' the previous populated column is less than AL
Set RecList(.Row) = RecList(.Row) + 1 ' continue recording, shift right, proceed with value assigning
Case Else ' exit if the previous populated column is AL
Exit Sub
End Select
.Parent.Cells(.Row, RecList(.Row)) = .Value ' assign the value from changed cell
End With
Next
End Sub

关于excel - 一个单元格中的值更改时的多个时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32440948/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com