【问题标题】:VBA to refresh Bloomberg data not running in proper orderVBA 刷新未按正确顺序运行的彭博数据
【发布时间】:2026-01-07 09:15:01
【问题描述】:

我的目的是更新彭博数据并使用不同的代码进行一些计算。但似乎 VBA 将运行所有计算而无需等待数据更新。 代码如下:

Application.Calculation = xlCalculationAutomatic

For i = 1 To 3

    Call Worksheets("Sheet1").Range("data1").Select 'the cells "data1" contains the function =BDH(ticker, field, start date, end date) to get the information from Bloomberg'

    Call Application.Run("RefreshCurrentSelection")

    Worksheets("sheet1").Range("d3").Value = Worksheets("sheet1").Range("sum") 'the cells "sum" takes the sum of all BB info'     

有人知道怎么解决吗?

【问题讨论】:

    标签: excel vba bloomberg


    【解决方案1】:

    Bloomberg 公式更新是异步的,因此您的代码不会等到更新结束。您需要将其余代码安排到稍后的时间,检查数据是否已更新。

    看起来像这样:

    Application.Run "RefreshCurrentSelection"
    update
    
    Sub update()
      If (check if the links have been updated) Then
        Worksheets("sheet1").Range("d3").Value = Worksheets("sheet1").Range("sum")
      Else
        Application.OnTime earliestTime:= Date + Time + timeserial(0, 0, 1), _
                       procedure:="update", Schedule:=True
      End if
    End Sub
    

    这将要求 Bloomberg API 刷新数据,然后等待 1 秒,检查数据是否已更新,如果没有更新,则再等一秒等,直到数据更新,然后它将运行范围分配。

    【讨论】:

      【解决方案2】:

      您必须在检查和刷新之间拆分它。

      Sub RefreshData()
      
      Application.Calculation = xlCalculationAutomatic
      
      Worksheets("Sheet1").Range("A1:A4").Select 'the cells "data1" contains the function =BDH(ticker, field, start date, end date) to get the information from Bloomberg'
      
      Application.Run "RefreshCurrentSelection"
      
      'Check to see if it filled
      Call Check_API
      
      End Sub
      
      Sub Check_API()
      
      If Application.WorksheetFunction.CountIfs(Range("A1:A4"), "#N/A Requesting Data...") > 0 Then
          'Check every 3 seconds
           Application.OnTime Now + TimeValue("00:00:03"), "Check_API"
      Else
      
          'What to do after API filled
           Worksheets("sheet1").Range("D3").Value = Application.WorksheetFunction.Sum(Worksheets("Sheet1").Range("A1:A4")) 'the cells "sum" takes the sum of all BB info'
      End If
      
      End Sub
      

      此外,您可以这样做,而不是专注于选择:

      根据默认选项设置刷新:

            Application.Run "RefreshData"
      

      刷新当前选择:

            Application.Run "RefreshCurrentSelection"
      

      刷新当前工作表:

            Application.Run "RefreshEntireWorksheet"
      

      刷新当前工作簿:

            Application.Run "RefreshEntireWorkbook"
      

      刷新所有工作簿:

            Application.Run "RefreshAllWorkbooks"
      

      如果你有兴趣。更好的选择是实现 v3 COM API 类,该类可以在 Bloomberg 的 SDK 中找到,带有 VBA 示例。

      【讨论】:

        【解决方案3】:

        即使您的 Call 看起来很奇怪,您也应该能够使用来自 Application.CalculationState 的值。

        还有其他方法可以“暂停”Excel,但您可以执行一个简单的 While 循环,除了等待 Application.CalculationState 的值变为 xlDone 之外什么都不做。

        Do Until Application.CalculationState=xlDone
        Loop
        

        【讨论】:

        • RefreshCurrentSelection 异步运行,因此无法正常工作。
        最近更新 更多