首页 > 解决方案 > 如何通过函数检测给定单元格值的变化

问题描述

我喜欢你给我的以下问题的解决方案:我有一个单元格中随机变化的财务数据,说A1,由财务软件提供,我想将这些数据一个接一个地记录在一个列中,我已经写了这个小代码,但它没有检测到 A1 值的变化,因为单元格 A1 包含软件提供的功能

    Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Range("A1"), Range(Target.Address)) Is Nothing Then
    Call test
End If
End Sub

Sub test()
Static n As Integer
n = Cells(1, 3).Value ' used for the faste verification of empty cells 
    While Not IsEmpty(Cells(n, 1))
        n = n + 1
    Wend
    Cells(n, 1).Value = Cells(1, 1).Value
    Cells(1, 3).Value = n
End Sub

标签: vba

解决方案


一种简单的方法是创建一个函数,将您要跟踪的单元格作为参数。这样,对单元格值(参数)的更改将生成对跟踪函数的调用。

像这样的东西将跟踪函数被调用的次数:

Dim i As Integer
Function tracker(c As Variant) As Integer
    i = i + 1
    tracker = i
End Function

推荐阅读