[英]Bloomberg Data taking time to load in Excel- VBA
我目前使用彭博 API 編寫了此 Excel VBA 代碼。 我正在嘗試使用 VBA 將 Bloomberg 中的數據提取到 Excel 中。 這工作正常,但問題是我有另一個宏,然后從彭博復制數據並將其粘貼到另一張表中。
如果數據還沒有全部從彭博輸出,那么我最終沒有足夠的數據來復制然后粘貼到另一張表中。
目前,我正在使用這行代碼:
Application.OnTime Now + TimeValue("00:01:45"), "RunAll"
在運行第一個宏 1 分 45 秒后等待,直到運行剩余的宏。 這很有用,但時間太多了。 問題是這大約是數據輸出所需的時間。
是否有其他更有效的方法可以通過確保數據更快地輸出到 excel 中來更快地提取彭博數據?
處理它的一種方法是當您啟動復制數據的第二個宏時,檢查中點單元格是否為空(類似於 A100??在這里查看您的代碼會有所幫助...)。 如果是,請等待 10 秒鍾並再次檢查。 這將迫使第二個宏保持保持模式,而第一個宏趕上。
不過請注意,我會設置最大循環數,否則如果由於某種原因沒有下載數據,它不會掛斷你的機器。
更新1:
我寫了下面的代碼來完成你想要做的事情。 您需要在當前代碼中進行一些操作,但我特意將其注釋得很重,因此您應該能夠遵循。 讓我知道這是否適合您。
Public boolBloombergCompleted As Boolean
Sub GetBloombergData()
'add this line after the data grab is complete
boolBloombergCompleted = True
End Sub
Sub WriteData()
Dim iRow As Integer
Dim boolTimeOut As Boolean
'change the last number as fit, make sure it's larger than the number of rows of data you're pulling in though
For iRow = 1 To 1000
' Check to see if the cell is blank
If Sheet1.Cells(iRow, 1) = vbNullString Then
' If the cell is blank and GetBloombergData has completed then exit sub
If boolBloombergCompleted = True Then
Exit Sub: Debug.Print "WriteData completed"
Else
' Call the wait function below
boolTimeOut = WaitForDataGrabToCatchUp(Sheet1.Cells(iRow, 1))
If boolTimeOut = True Then GoTo TimeOutErr:
End If
End If
' < Add your code to write data in here >
Next iRow
Exit Sub
TimeOutErr:
MsgBox "The write sub timed out while waiting for data to load", vbExclamation
End Sub
Function WaitForDataGrabToCatchUp(rng As Range) As Boolean
Dim StartTime1 As Long
Dim StartTime2 As Long
Dim PauseTime As Long
Dim StopTime As Long
' Set the amount of time to pause between checking the spreadsheet for updates
PauseTime = 5 'seconds
' Set the maximum amount of time to wait before timing out
StopTime = 60 'seconds
' StartTime1 is used for calculating overall time
StartTime1 = Timer
Do While rng = vbNullString
' check if the StopTime has been reached
If Timer - StartTime1 > StopTime Then
WaitForDataGrabToCatchUp = True
Exit Function
Else
' loop for amount of PausedTime (the DoEvents part here is key to keep the data grab moving)
StartTime2 = Timer
Do While Timer < StartTime2 + PauseTime
Debug.Print Timer - StartTime1
DoEvents
Loop
End If
Loop
WaitForDataGrabToCatchUp = False ' means it did not time out
End Function
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.