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

时间:2016-04-28 20:33:59

标签: excel vba excel-vba

这是一个更大的宏的一部分,它有多个Application.OnTime实例,可以正常工作。

下面这个问题出现在WaitForPriceVolume(),当它到达For Each循环且If为真时,它不会返回到WaitForPriceVolume()过程}。它会回到之前调用的所有过程,实际上只是执行Exit Sub,就好像OnTime不存在一样。

当我删除下面的代码并为正在使用的全局变量添加固定值时,Application.OnTime有效。只有当我把它插回到更大的宏时才会这样。

    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

1 个答案:

答案 0 :(得分:0)

自己愚蠢。 OnTime的所有其他实例都出现在代码的末尾,因此在触发OnTime之前,宏没有任何事情要做,我强迫所有内容循环回主宏。在这种情况下,我没有这样做。问题解决了。这困扰了我一个星期