善良的圣母,我已经明白了。
这不是一个完美的解决方案,可能有点慢,但至少它有效。
有人(我最终会)应该能够改进它,以便它可以处理多单元格范围。本质上它会等待每个单元格依次完成计算。看来我们使用的大多数 PP 查找公式都会分批完成,因此只需要每批中的一个单元格进行测试。它相当有效,但它绝对可以利用优化。当我改进它时我会回复。
Option Explicit
Option Compare Text
Function PP_Calcs_Finished() As Boolean
'v9.00 2016-11-28 10:39 - added PP_Calcs_Finished
'test for PowerPivot calculations to be completed
'tests any range names starting with prefix "PP_test_" to look for #GETTING_DATA in cell text
Const cPPwait As String = "#GETTING_DATA"
'choose various cells in workbook and label ranges with prefix "PP_test_" to be checked for completion
Const cPPprefix As String = "PP_test_"
'runs itself once per sRepeat seconds until test completes, this allows calcs to run in background
Const sRepeat As Byte = 2
'Result: True means OK, False means not OK
Application.StatusBar = "PLEASE NOTE: readjusting lookups and formulas in the background, please be patient..."
'ensure calculations are automatic
Application.Calculation = xlCalculationAutomatic
Dim nm As Name, test_nm() As Name, n As Integer, nmax As Integer, ws As Worksheet
'find all test ranges
nmax = 0
'workbook scope
For Each nm In ThisWorkbook.Names
If Left(nm.Name, 8) = cPPprefix Then
nmax = nmax + 1
ReDim Preserve test_nm(1 To nmax) As Name
Set test_nm(nmax) = nm
End If
Next nm
'worksheet scope
For Each ws In Worksheets
For Each nm In ws.Names
If Left(nm.Name, 8) = cPPprefix Then
nmax = nmax + 1
ReDim Preserve test_nm(1 To nmax) As Name
Set test_nm(nmax) = nm
End If
Next nm
Next ws
'now test all ranges
Dim sSheetName As String, sRangeName As String
If nmax > 0 Then
For n = 1 To nmax
sSheetName = Mid(test_nm(n).RefersTo, 2, InStr(1, test_nm(n).RefersTo, "!") - 2)
sRangeName = Mid(test_nm(n).RefersTo, InStr(1, test_nm(n).RefersTo, "!") + 1, 500)
If Worksheets(sSheetName).Range(sRangeName).Cells(1).Text = cPPwait Then
'still waiting, quit and test again in sRepeat seconds
Application.OnTime Now + TimeSerial(0, 0, sRepeat), "PP_Calcs_Finished"
Exit Function
End If
Next n
End If
Application.StatusBar = False
PP_Calcs_Finished = True
'Application.Calculate
End Function