我编写了一个代码来显示单元格的旧值和新值,然后将消息框显示数据一个接一个地存储到另一个表单中......
Option Explicit
Dim OldVals As New Dictionary
Private Sub Worksheet_Change(ByVal Target As Range)
Dim myCell As Range
Dim sMsg As String
Dim rg As Range
Dim lr As Integer
For Each myCell In Target
If OldVals.Exists(myCell.Address) Then
sMsg = "New value of " & Replace(myCell.Address, "$", "") & " is " & myCell.Value & "; old value was " & OldVals(myCell.Address)
MsgBox sMsg
If MsgBox(sMsg) = vbOK Then Set rg = ThisWorkbook.Sheets("Sheet2").Range("A1")
lr = rg.CurrentRegion.Rows.Count
rg.Offset(lr, 0).Value = sMsg
Else
MsgBox "No old value for " + Replace(myCell.Address, "$", "")
End If
OldVals(myCell.Address) = myCell.Value
Next myCell
End Sub
在我的代码中,当我在输入单元格值后按Enter键时,消息框将出现并显示旧的和新的值,如下图所示。
但是我想在按ctrl + s键而不是按回车键或任何其他键后保存单元格值后才显示此消息框。 只有在按 Ctrl + s 保存单元格值后,消息框才会如上所示,但我无法理解这一点我