我有“Sheet10”,其中单元格“F3”根据用户输入进行一些计算和值更改。用户输入位于“Sheet10”单元格“G6”上。我想复制“F3”的最终计算值,然后将新值更改为“Sheet12”单元格“Q3”下面是我的代码,但我不确定我哪里出错了。 提前致谢!
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("F3").Address Then
Worksheets("Sheet12").Range("Q3") = Target.Value
End If
End Sub
答案 0 :(得分:1)
之前已经问过这个问题了,但我现在懒得搜索它......
由于Worksheet_Change
事件在单元格发生变化后触发,Target.Value
将反映新值。但是,Undo
命令可用于通过将新值存储到变量中来运行撤消,然后使用新值更新目标并将旧值存储在所需位置来获取旧值。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("G6").Address Then
Application.EnableEvents = False
Dim sOldValue As String, sNewValue As String
sNewValue = Target.Value
Application.Undo
sOldValue = Range("F3").Value
Target.Value = sNewValue
Worksheets("Sheet12").Range("Q3") = sOldValue
Application.EnableEvents = True
End If
End Sub
对于多个单元格,请执行以下操作:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("G6").Address Then
Application.EnableEvents = False
Dim sOldValue As String, sNewValue As String
sNewValue = Target.Value
Application.Undo
Dim rOld as Range
Set rOld = Range("F3:V3").Value
Target.Value = sNewValue
Range("V8:AD8").Value = rOld.Value
Application.EnableEvents = True
End If
End Sub