my purpose is to update Bloomberg data and do some calculatations with different tickers. But it seems that VBA will run all the calculations without waiting the data to be
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
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.
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.