首页 > 解决方案 > 使用数据时间戳和值跟踪单元格的值变化

问题描述

我目前正在开发 Excel VBA 上的需求管理工具。

在 GI 列中有需求的状态(待办事项、待处理、正在进行的工作、已完成),在 MI 列中希望在用户更改值时自动跟踪状态的变化。

我想要获得的是类似“状态从”& [state t] & “to” & [state t+1] & “on” & [timestamp] 示例:“状态从正在进行的工作更改为完成2019/09/05 - 15:30"

我是 VBA 的初学者,所以我不知道从哪里开始这项任务......

提前致谢!

标签: excelvba

解决方案


You need to attach code to the sheet_change event. This is called every time the sheet changes. The code needs to check if the cell that changed is in your target column (G) and if so, write the appropriate value into the adjacent cell in column M. To attach code to an event it has to be in the sheet module of the appropriate sheet. Start the VB editor by pressing Alt-F11. In the project explorer double click on the appropriate sheet to open up the sheet module. Paste the following code into that module.

Private Sub Worksheet_Change(ByVal Target As Range)
if target.column = 7 then 'if column G then...
   Application.EnableEvents = false 'stop tracking changes
   Cells(target.row,13) = "State changed on " & format(date(),"yyyy/mm/dd hh:mm")
   Application.EnableEvents = true 
End If
End Sub

推荐阅读