Bloomberg数据需要时间加载Excel-VBA

时间:2016-07-07 21:08:30

标签: vba performance macros bloomberg

enter image description here我目前使用bloomberg数据编写代码。我正在尝试使用VBA将Bloomberg的数据导入excel。这工作正常,但问题是我有另一个宏,然后从bloomberg复制数据并将其粘贴到另一张表。

如果数据还没有全部从bloomberg输出,那么我最终没有足够的数据可以复制,然后粘贴到另一张纸上。

目前,我正在使用以下代码行:Application.OnTime Now + TimeValue(“00:01:45”),“RunAll”

在运行第一个宏1分45秒后等待,直到它运行剩余的宏。它很有用,但很长时间。问题在于这是数据输出所需的时间。

通过确保数据更快地输出到Excel中,还有其他更有效的方式让我更快地获取bloomberg数据吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

处理它的一种方法是当你启动复制数据的第二个宏时,检查一下中点单元格是否为空(类似于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
相关问题