我目前正在尝试获取有关积压工作进展情况的历史信息。
我的 Excel 文件基于 Access 数据库的查询,它可以让我了解当前情况。
我想在每次周数变化时自动运行宏。我目前正在使用以下代码:
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
应该触发的宏称为 KPIupdate
我的问题是,只有当我单击单元格时,宏才会触发。我希望它在数字变化时触发。单元格“D3”通过公式 =Weeknum(Today();21)
链接到另一个单元格希望你能帮助我
最佳答案
根据MSDN Worksheet_Change
条目:
This event does not occur when cells change during a recalculation. Use the Calculate event to trap a sheet recalculation.
要使用 Worksheet_Calculate
捕获通过查看另一个单元格的公式设置的单元格中的更改,您需要设置一个变量来保存“目标”的值,然后检查是否它在 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
要对此进行测试,只需将 A1
中的公式设置为 =B1
,然后更改 B1
的值并检查输出立即窗口。
您可以修改此代码以在我的 Debug.Print...
语句所在的位置调用 KPIupdate
。
关于vba - 当链接的单元格更改值时运行宏(Excel VBA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741967/