VBA使用多个Application.OnTime; 一个似乎失败了

这是一个更大的macros的一部分,有多个Application.OnTime实例工作得很好。

我在下面的这个问题是在WaitForPriceVolume()当它到达For Each循环,并且If是真的,它不回到过程WaitForPriceVolume() 。 它回到之前调用的所有过程,实际上就像Exit Sub OnTime不存在一样。

当我删除只是下面的代码,并为所使用的全局variables添加固定值时, Application.OnTime作品。 只有当我把它插回到更大的macros观中。

  Sub BDP_PriceVolume() Dim lsStartRange As String Dim lsEndRange As String Dim lnStartRow As Long Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic Set sht = Worksheets("Variables") ' Use gvList lsStartRange = "C" & gnStartRow lnStartRow = gnStartRow + UBound(gvList, 2) lsEndRange = "C" & lnStartRow sht.Range(lsStartRange & ":" & lsEndRange).Value = _ "=BDP($A" & gnStartRow & "&Variables!$A$2,Variables!$D$2)" lsStartRange = "D" & gnStartRow lsEndRange = "D" & lnStartRow If Worksheets("Variables").Cells(3, 3).Value <> "" Then sht.Range(lsStartRange & ":" & lsEndRange).Value = _ "=BDH($A" & gnStartRow & "&Variables!$A$2,Variables!$E$3" & "," & _ "Variables!$B$4,Variables!$C$3," & _ Chr(34) & "BarTp=T" & Chr(34) & "," & _ Chr(34) & "BarSz=40" & Chr(34) & "," & _ Chr(34) & "Dir=V" & Chr(34) & "," & _ Chr(34) & "Dts=H" & Chr(34) & "," & _ Chr(34) & "Sort=A" & Chr(34) & "," & _ Chr(34) & "Quote=C" & Chr(34) & "," & _ Chr(34) & "UseDPDF=Y" & Chr(34) & ")" Else sht.Range(lsStartRange & ":" & lsEndRange).Value = _ "=BDP($A" & gnStartRow & "&Variables!$A$2,Variables!$E$2)" End If sht.Range("C" & gnStartRow & ":" & lsEndRange).Select Application.Run "RefreshCurrentSelection" Application.OnTime Now + TimeValue("00:00:03"), "WaitForPriceVolume" End Sub Private Sub WaitForPriceVolume() Dim rng As Range Set rng = sht.Range("C" & gnStartRow & ":D" & fnLastRow(sht, "A")) Dim cell As Range Application.ScreenUpdating = True For Each cell In rng If cell.Value = "#N/A Requesting Data..." Then Application.OnTime Now + TimeValue("00:00:03"), "WaitForPriceVolume" Exit Sub End If Next cell Call DoneWaitForPriceVolume End Sub 

自己的愚蠢。 OnTime的所有其他实例都出现在代码的末尾,所以在OnTime触发之前,macros已经没有什么可做的事了,我强迫所有的东西都回到主macros。 在这种情况下,我没有这样做。 问题解决了。 这让我困扰了一个星期