且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VBA刷新未按正确顺序运行的彭博数据

更新时间:2023-12-05 07:54:40

您必须在检查和刷新之间进行拆分.

You have to split it up between a check and a refresh.

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

此外,您可以执行以下操作,而不是专注于选择:

Also, instead of focusing on the selection, you can do:

根据默认选项设置刷新:

Refresh based on default option setting:

      Application.Run "RefreshData"

刷新当前选择:

      Application.Run "RefreshCurrentSelection"

刷新当前工作表:

      Application.Run "RefreshEntireWorksheet"

刷新当前工作簿:

      Application.Run "RefreshEntireWorkbook"

刷新所有工作簿:

      Application.Run "RefreshAllWorkbooks"

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

If you were interested. Still the better option is implementing the v3 COM API class that can be found in Bloomberg's SDK with VBA examples.