我的目的是更新Bloomberg数据并使用不同的代码进行一些计算。但似乎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'
任何人都知道如何修复它?
答案 0 :(得分:1)
您必须在检查和刷新之间将其拆分。
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示例。
答案 1 :(得分:0)
即使您的致电看起来很奇怪,您也应该能够使用Application.CalculationState
中的值。
还有其他方法可以暂停" Excel,但是您可以执行一个简单的While循环,除了等待Application.CalculationState
的值变为xlDone
之外什么都不做。
Do Until Application.CalculationState=xlDone
Loop
答案 2 :(得分:0)
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秒钟,检查数据是否更新,如果没有则等待另一秒等等,直到数据更新,然后它将运行范围分配。