我有以下代码,用于记录工作表中的更改:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strAddress As String
Dim val
Dim dtmTime As Date
Dim Rw As Long
Dim x As String
Sheets("Shipment").Select
If Intersect(Target, Range("D3:D200,K3:K200")) Is Nothing Then Exit Sub
dtmTime = Now()
val = Target.Value
strAddress = Target.Address
x = Cells(2, Target.Column).Value
Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("Log Sheet")
.Cells(Rw, 1) = strAddress
.Cells(Rw, 2) = val
.Cells(Rw, 3) = dtmTime
.Cells(Rw, 4) = x
End With
End Sub
这只显示了我的新值,我也希望看到该单元格的旧值。这是我需要帮助的地方。如果有人能告诉我如何合并一列来显示更改的单元格的旧值。
答案 0 :(得分:0)
首先,复制D列并将其粘贴到DD列
中然后复制下面的代码
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strAddress As String
Dim val
Dim dtmTime As Date
Dim Rw As Long
Dim x As String
Sheets("Shipment").Select
If Intersect(Target, Range("D3:D200,K3:K200")) Is Nothing Then Exit Sub
dtmTime = Now()
val = Target.Value
Old_val = Sheets("Shipment").Range("DD" & Target.Row)
strAddress = Target.Address
x = Cells(2, Target.Column).Value
Rw = Sheets("Log Sheet").Range("A" & Rows.Count).End(xlUp).Row + 1
With Sheets("Log Sheet")
.Cells(Rw, 1) = strAddress
.Cells(Rw, 2) = val
.Cells(Rw, 3) = Old_val
.Cells(Rw, 4) = dtmTime()
.Cells(Rw, 5) = x
End With
Sheets("Shipment").Range("DD" & Target.Row) = Target.Value
End Sub
希望这是你想要的。 :)
答案 1 :(得分:0)
完整的解决方案将涉及创建“发货”工作表的副本,以使用以前值的基线。以下是设置方法:
Worksheet_Change
事件。)Worksheet_Change
事件代码中,当您确定已更改的目标单元格时,您可以轻松地从“Shipment-OldValues”工作表中获取上一个值。 我自己的建议是隐藏“Shipment-OldValues”工作表,因为它会减少对任何用户的混淆。