【问题标题】:Bloomberg Data taking time to load in Excel- VBA彭博数据需要时间加载到 Excel-VBA
【发布时间】:2016-07-07 21:08:29
【问题描述】:

我目前使用彭博 API 编写 this Excel VBA code。我正在尝试使用 VBA 将数据从 Bloomberg 提取到 Excel 中。这工作正常,但问题是我有另一个宏,然后从 Bloomberg 复制数据并将其粘贴到另一张表中。

如果数据还没有全部从 Bloomberg 输出,那么我最终没有足够的数据来复制然后粘贴到另一张表中。

目前,我正在使用这行代码:

Application.OnTime Now + TimeValue("00:01:45"), "RunAll"

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

是否有任何其他更有效的方法可以通过确保数据更快地输出到 Excel 中来更快地提取彭博数据?

【问题讨论】:

  • 加载单元格是否包含特定字符串?也许你可以运行一个循环或拦截 Worksheet.Change 事件来检查这个字符串。
  • 请提供示例代码。我不知道彭博数据是什么,所以请让问题更笼统。你是自动化 IE 来获取数据吗?您正在建立数据库连接吗?
  • 如何检索数据?如果您以编程方式执行此操作,则不必像这样等待...
  • 我附上了上面代码的照片。首先,我将彭博数据加载到 excel 中。之后,当人们重新打开文件时,他们可以清除工作表内容并刷新工作表以提取数据以防数据发生变化。刷新后,我放置了一个等待 1 分 45 秒的计时器,直到它运行剩余的宏。这是我想尝试并找到一种方法来确保数据以更快的速度完全刷新和输出的地方,但我不知道如何。谢谢!

标签: excel vba performance macros bloomberg


【解决方案1】:

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

【讨论】:

  • 我附上了上面代码的照片。首先,我将彭博数据加载到 excel 中。之后,当人们重新打开文件时,他们可以清除工作表内容并刷新工作表以提取数据以防数据发生变化。刷新后,我放置了一个等待 1 分 45 秒的计时器,直到它运行剩余的宏。这是我想尝试并找到一种方法来确保数据以更快的速度完全刷新和输出的地方,但我不知道如何。谢谢!
  • 谢谢菲利普,还没有尝试过,因为代码有点难以理解(我对 VBA 很陌生)。如果单元格为空白,为什么它会退出子程序?我的问题是,当我刷新工作表时,包含 BBRG 公式的单元格会显示 N/A Requesting Data,有时可能会使用公式加载单元格,但在输出剩余行中的剩余信息时会滞后在公式所在的单元格下。
  • 只有两种方式退出子 (1) 它所在的单元格是空白的并且所有数据都被拉入,这意味着没有更多数据,或者 (2) 你已达到您愿意等待的最大时间。我正在检查单元格是否为空白,因为上面您提到“他们可以清除工作表内容并刷新工作表以提取数据”。如果他们清除这张纸,我会认为这意味着它是空的?同样,如果您可以发布您的代码,我可以为您提供更完整的解决方案。
  • 对不起!我以为我发了一张照片。刚刚做了
  • 我没有看到你在哪里提取数据,看起来它只是在粘贴公式。此外,在 CUSIP_Copy_Paste 中,您仅使用“单元格”而不指示工作表名称,这不是正确的方法,只会在以后导致问题。为了使用我上面的内容,您需要重新设计该模块,否则它根本无法工作。以上代码要做的就是在复制该行之前检查该行的第一个单元格中是否有值。如果你牢记这一点,你应该能够返工。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多