暂停 VBA 循环直到 sheet 被重新计算

Pause VBA loop until sheet is recalculated

我搜索了 Google 直到第 10 页,但找不到解决方案。 我在 VBA 中有一个循环,但希望它等到 sheet 重新计算后再继续。

大多数人的建议是雇用 DoEvents。但是,这对我不起作用。

到目前为止,这是我的代码,它不会等到 sheet 计算出来:

Sub Replaceifrebalance() 
Dim x As Integer 

NumRows = Range("CF16", Range("CF16").End(xlDown)).Rows.Count 

Range("CF16").Select 

For x = 1 To NumRows 
    If Range("CF" & x).Value > 0 Then 
        Range("AW15:BF15").Select 
        Application.CutCopyMode = False 
        Selection.Copy 
        Range("AW1").Select 
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ 
        :=False, Transpose:=False 
        Range("AW" & 1 & ":BF" & 1).Copy Worksheets("Timeseries").Range("BI" & x & ":BR" & x) 

        Application.Calculate 
        If Not Application.CalculationState = xlDone Then 
            DoEvents 
        End If 
    End If 

Next 

End Sub 

有谁知道与此不同的解决方案吗?:

  Application.Calculate 
    If Not Application.CalculationState = xlDone Then 
        DoEvents 
    End If 

使用Do WhileIf没有用。对于 If 代码将评估一次然后继续前进。 Do While 将一直循环直到满足条件。

Do
    DoEvents
Loop While Not Application.CalculationState = xlDone