链接的单元格更改值时运行macros(Excel VBA)

我目前正在试图获得关于积压是如何发展的历史信息。

我的Excel文件基于来自Access数据库的查询,可以让我查看当前的情况。

每当周数变化时,我想自动运行一个macros。 我目前使用下面的代码:

Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Target.Worksheet.Range("D3")) Is Nothing Then Call KPIupdate End If End Sub 

应该触发的macros被称为KPIupdate

我的问题是,只有单击单元格时,macros才会触发。 我希望它只是在数字发生变化时才开火。 单元格“D3”链接到公式为= Weeknum(Today(); 21)的另一个单元格

我希望你可以帮助我

根据Worksheet_Change的MSDN条目:

在重新计算过程中单元格更改时,不会发生此事件。 使用“计算”​​事件来捕获表单重新计算。

要使用Worksheet_Calculate来捕获由公式设置的单元格中的更改,并查看另一个单元格,则需要设置一个variables来保存“目标”的值,然后检查Calculate事件触发后是否更改。

这是一个简单的例子:

 Option Explicit Private strCurrentWeek As String Private Sub Worksheet_Calculate() If Me.Range("A1").Value <> strCurrentWeek Then 'the linked cell changed Debug.Print "Sheet1!A1 was changed" 'call another macro End If 'update the new current week strCurrentWeek = Me.Range("A1").Value End Sub 

为了testing这个,只需将A1的公式设为=B1 ,然后更改B1的值并在立即窗口中检查输出。

您可以调整此代码以调用我的Debug.Print...语句所在的KPIupdate