在vba完成运行之前不会填充彭博数据

时间:2019-02-21 13:17:40

标签: excel vba bloomberg

我知道过去也曾问过类似的问题,但通过这些帖子,我仍无法找到以下问题的解决方案。

我有2个使用Bloomberg API公式的潜艇。在第二个(Setup_2)中,变量LastRow1依赖于已填充为可以正常工作的Setup_1。

使用checkStatus_1和checkStatus_2可以分别运行2个设置子程序,但是当我尝试创建一个单独的子程序调用它们时,它不起作用,因为LastRow1所依赖的数据不存在。

以下是相关代码:

Sub Setup_1()
    Dim ws1 As Worksheet
    Set ws1 = Worksheets("Returns")
    ws1.Cells(2, 1).Formula = "=BDS(Control!B4,""INDX_MWEIGHT_HIST"",""END_DATE_OVERRIDE"",TEXT($A$1,""YYYYMMDD""))"
    ws1.Cells(1, 4).Formula = "=BDH(A2&"" Equity"",""DAY_TO_DAY_TOT_RETURN_GROSS_DVDS"",$B$1,$A$1,""dir=h"")"
    ws1.Cells(3, 4).Formula = "=BDH(A3&"" Equity"",""DAY_TO_DAY_TOT_RETURN_GROSS_DVDS"",$B$1,$A$1,""dir=h"",""dts=h"")"
    checkStatus_1
End Sub

Sub Setup_2()
    Dim ws1 As Worksheet
    Set ws1 = Worksheets("Returns")
    Dim LastRow1 As Long
    LastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    ws1.Cells(3, 4).Formula = "=BDH(A3&"" Equity"",""DAY_TO_DAY_TOT_RETURN_GROSS_DVDS"",$B$1,$A$1,""dir=h"",""dts=h"")"
    ws1.Cells(3, 4).AutoFill Destination:=ws1.Range(ws1.Cells(3, 4), ws1.Cells(LastRow1, 4))
    checkStatus_2
End Sub

Sub Setup_3()
    Dim ws1 As Worksheet
    Set ws1 = Worksheets("Returns")
    Dim LastRow1 As Long
    Dim LastCol1 As Long
    Dim LCol As String
    LastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    LastCol1 = ws1.Cells(1, Columns.Count).End(xlToLeft).Column
    LCol = Split(Cells(, LastCol1).Address, "$")(1)
    ws1.Cells(2, 3).Formula = "=(STDEV.S(D2:" & LCol & "2)*SQRT(252))/100"
    ws1.Cells(2, 3).AutoFill Destination:=ws1.Range(ws1.Cells(2, 3), ws1.Cells(LastRow1, 3))
End Sub

Sub checkStatus_1()
    Dim ws1 As Worksheet
    Dim rng As Range
    Dim c As Range
    Set ws1 = Worksheets("Returns")
    Set rng = Application.Union(ws1.Cells(2, 1), ws1.Cells(1, 4), ws1.Cells(3, 4))
    For Each c In rng
        If "#N/A Requesting Data..." = c Or "#N/A Invalid Securiity" = c Then
            Application.OnTime (Now + TimeValue("00:00:02")), "checkStatus_1"
            Exit Sub
        End If
    Next c    
End Sub

Sub checkStatus_2()
    Dim ws1 As Worksheet
    Dim rng As Range
    Dim c As Range
    Dim LastRow1 As Long
    Set ws1 = Worksheets("Returns")
    LastRow1 = ws1.Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = ws1.Range(ws1.Cells(3, 4), ws1.Cells(LastRow1, 4))
    For Each c In rng
        If "#N/A Requesting Data..." = c Then
            Application.OnTime (Now + TimeValue("00:00:02")), "checkStatus_2"
            Exit Sub
        End If
    Next c    
End Sub

0 个答案:

没有答案