如何在 Excel VBA 中获取已更改单元格的旧值? [英] How do I get the old value of a changed cell in Excel VBA?

查看:62
本文介绍了如何在 Excel VBA 中获取已更改单元格的旧值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样检测 Excel 电子表格中某些单元格值的变化......

I'm detecting changes in the values of certain cells in an Excel spreadsheet like this...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Dim old_value As String
Dim new_value As String

For Each cell In Target

    If Not (Intersect(cell, Range("cell_of_interest")) Is Nothing) Then
        new_value = cell.Value
        old_value = ' what here?
        Call DoFoo (old_value, new_value)
    End If

Next cell

End Sub

假设这不是太糟糕的编码方式,我如何在更改之前获取单元格的值?

Assuming this isn't too bad a way of coding this, how do I get the value of the cell before the change?

推荐答案

试试这个

声明一个变量 say

Dim oval

并在 SelectionChange 事件

Public Sub Worksheet_SelectionChange(ByVal Target As Range)
oval = Target.Value
End Sub

并在您的 Worksheet_Change 事件集中

old_value = oval

这篇关于如何在 Excel VBA 中获取已更改单元格的旧值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆