一个单元格中的值发生更改时的多个时间戳

我试图logging在列A中的一个单元格中所做的多个更改。在我的示例中,我有一个用户将在A5列inputdate和时间。 稍后用户可能会更改同一单元格中的值(A5)。 这个变化可能会发生5次。 如何logging从AH栏开始的所有这些更改的date和时间。

所以A5中的第一个变化应该logging在AH5列中,A5中的第二个变化应该logging在AI5列中,以此类推。

我发现了多个macros,但它们只是每次在同一个单元格中更改date和时间。

将下面的代码放到您的目标工作表模块中:

 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 

工作表更改事件处理程序

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

UPDATE

这是满足你最后要求的代码。 为了使它足够灵活,我添加了一些额外的检查,禁止logging的价值,如果它不是一个date或以前logging相同。 您可以通过删除相应的Case语句行来轻松更改这些限制 – 请参阅我的意见。 它也处理所有更改的单元格,例如复制的单元格已粘贴到多个选定的单元格。

 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