提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何在Excel中记录单元格中的变化值?

如何在Excel中记录频繁变化的单元格的每个变化值? 例如,单元格C2中的原始值为100,将数字100更改为200时,原始值100将自动显示在单元格D2中进行记录。 继续将200更改为300,将数字200插入到单元格D3中,将300更改为400将将300显示为D4等等。 本文中的方法可以帮助您实现它。

用VBA代码记录单元格中的变化值

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

用VBA代码记录单元格中的变化值


下面的VBA代码可以帮助您记录Excel中单元格中的每个变化值。 请做如下。

1。 在工作表中包含要记录更改值的单元格,右键单击工作表选项卡,然后单击 查看代码 从上下文菜单。 看截图:

2。 那么 Microsoft Visual Basic for Applications 窗口正在打开,请将以下VBA代码复制到代码窗口中。

VBA代码:记录单元格中的变化值

Dim xVal As String
'Update by Extendoffice 2018/8/22
Private Sub Worksheet_Change(ByVal Target As Range)
    Static xCount As Integer
    Application.EnableEvents = False
    If Target.Address = Range("C2").Address Then
        Range("D2").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
    Else
        If xVal <> Range("C2").Value Then
         Range("D2").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
        End If
    End If
    Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    xVal = Range("C2").Value
End Sub

笔记:

1。 在代码中,C2是要记录其所有更改值的单元格。 D2是您将填充C2第一个变化值的单元格。

3。 按 其他 + Q 键关闭 Microsoft Visual Basic for Applications 窗口。

从现在起,每次当您更改单元格C2中的值时,以前的更改值都将记录在D2和D2下面的单元格中。


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    marcin · 1 months ago
    it works when I type in the data, it doesn't work when I stream real time data to this cell directly (=RTD(.....)). how can I make it work with RTD ?
  • To post as a guest, your comment is unpublished.
    krishna · 3 months ago
    I tried this code for the C2 cell which contatins DDE values which changes second by second. I use this following code but not working.

    Dim xVal As String
    Private Sub Worksheet_Change(ByVal Target As Range)
    Static xCount As Integer
    Application.EnableEvents = False
    If Target.Address = Range("C2").Address Then
    Range("D2").Offset(xCount, 0).Value = xVal
    xCount = xCount + 1
    Else
    If xVal <> Range("C2").Value Then
    Range("D2").Offset(xCount, 0).Value = xVal
    xCount = xCount + 1
    End If
    End If
    Application.EnableEvents = True
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    xVal = Range("C2").Value
    End Sub
  • To post as a guest, your comment is unpublished.
    Marky Mark · 4 months ago
    Try This

    Dim xVal As String
    Dim iVal As Integer
    Private Sub Worksheet_Change(ByVal Target As Range)
    Static xCount As Integer
    iVal = Application.WorksheetFunction.Count(Range("F:F"), 1)
    xCount = iVal
    Application.EnableEvents = False
    If Target.Address = Range("C2").Address Then
    Range("E3").Offset(xCount, 0).Value = Range("C2").Value
    Range("F3").Offset(xCount, 0).Value = Now
    xCount = xCount + 1
    Else
    If xVal <> Range("C2").Value Then
    Range("E3").Offset(xCount, 0).Value = Range("C2").Value
    Range("F3").Offset(xCount, 0).Value = Now
    xCount = xCount + 1
    End If
    End If
    Application.EnableEvents = True
    End Sub
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    xVal = Range("C2").Value
    End Sub
  • To post as a guest, your comment is unpublished.
    Jackie · 5 months ago
    Hi! Thanks for the code, but I have a question

    Is there a way to modify the code, such that it records the value if the cell daily, or on certain days, which I specify in a separate column?


    Thanks!
  • To post as a guest, your comment is unpublished.
    Anthony · 6 months ago
    Hello, Would it be possible to apply this for more than one cell ?
  • To post as a guest, your comment is unpublished.
    John · 7 months ago
    How do I save the number as soon as it is generated not after a new number is generated? The problem I am having is the number is not recorded right away but after a second number is created; this means neither the cell that I am recording or the cell that is the target have the number...how can I record as soon as the number is generated? Thanks for your help!
  • To post as a guest, your comment is unpublished.
    Yusuf · 9 months ago
    Peki bu kaydı yatay olarak nasıl kaydedeceğiz. Satırlara değil Sütunlara kaydetmesini istiyorum. Teşekkürler
  • To post as a guest, your comment is unpublished.
    Abdallah · 9 months ago
    Thanks

    but what can i do if i need to repeat it for a raw
  • To post as a guest, your comment is unpublished.
    Abdallah · 9 months ago
    Thanks

    but what can i do if i need to repeat it for many cells
  • To post as a guest, your comment is unpublished.
    wayne · 10 months ago
    thank you for this but instead of going on forever how could i restart back at first cell after X amount of times?
    • To post as a guest, your comment is unpublished.
      crystal · 9 months ago
      Hi,
      Do you mean after recording X mount of times, you want to restart back to the first record value?
      Sorry I am not sure I got your question. Would be nice if you could provide screenshot of what you are trying to do.
  • To post as a guest, your comment is unpublished.
    Maybe · 1 years ago
    Hello, Would it be possible for this macro to record two seperate cells in two seperate columns? Ie. Can I record All values from A1 in Column B and all Values of C1 in column D?
  • To post as a guest, your comment is unpublished.
    Xy · 1 years ago
    What if cell C2 is a formula? How do I record the values of C2 if it is a formula?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Good Day,
      The code has been optimized. Please have a try and thanks for your comment.

      Dim xVal As String
      Private Sub Worksheet_Change(ByVal Target As Range)
      Static xCount As Integer
      Application.EnableEvents = False
      If Target.Address = Range("C2").Address Then
      Range("D2").Offset(xCount, 0).Value = xVal
      xCount = xCount + 1
      Else
      If xVal <> Range("C2").Value Then
      Range("D2").Offset(xCount, 0).Value = xVal
      xCount = xCount + 1
      End If
      End If
      Application.EnableEvents = True
      End Sub
      Private Sub Worksheet_SelectionChange(ByVal Target As Range)
      xVal = Range("C2").Value
      End Sub
  • To post as a guest, your comment is unpublished.
    Jorge Jaramillo · 1 years ago
    Hi


    This works really well if the value in C2 is entered each time, but it doesn't work if C2 contains a formula. Is there a way to this same thing but with a formula in C2?


    Thanks for this easy solution.
    • To post as a guest, your comment is unpublished.
      Andrew K · 1 years ago
      If you find out how to use it if C2 contains a formula will you please please let me know how you did it. I can't seem to find how to anywhere on the internet.
      • To post as a guest, your comment is unpublished.
        crystal · 1 years ago
        Good Day,
        The code has been optimized. Please have a try and thanks for your comment.

        Dim xVal As String
        Private Sub Worksheet_Change(ByVal Target As Range)
        Static xCount As Integer
        Application.EnableEvents = False
        If Target.Address = Range("C2").Address Then
        Range("D2").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
        Else
        If xVal <> Range("C2").Value Then
        Range("D2").Offset(xCount, 0).Value = xVal
        xCount = xCount + 1
        End If
        End If
        Application.EnableEvents = True
        End Sub
        Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        xVal = Range("C2").Value
        End Sub