VBA to refresh Bloomberg data not running in proper order

不羁岁月 提交于 2019-11-28 10:37:46

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"

Refresh current selection:

      Application.Run "RefreshCurrentSelection"

Refresh current worksheet:

      Application.Run "RefreshEntireWorksheet"

Refresh current workbook:

      Application.Run "RefreshEntireWorkbook"

Refresh all workbooks:

      Application.Run "RefreshAllWorkbooks"

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.

Even though your Calls seems odd, you should be able to make use of the value from Application.CalculationState.

There are other ways to "pause" Excel, but you can do a simple While loop that do nothing except waiting for value of Application.CalculationState becomes xlDone.

Do Until Application.CalculationState=xlDone
Loop

Bloomberg formulae update is asynchronous so your code won't wait until the update is over. You need to schedule the rest of your code to a later time, check if the data has been updated.

It would look like this:

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

That would ask the Bloomberg API to refresh the data, then wait 1 second, check if the data is updated, wait another second if not etc. until the data is updated and then it would run the range assignment.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!