2016-07-07 188 views
0

enter image description here我目前有一个使用bloomberg数据编写的代码。我正试图使用​​VBA将彭博的数据转化为excel。这工作正常,但问题是,我有另一个宏,然后将数据从bloomberg中复制并粘贴到另一个表中。彭博数据抽取时间在Excel中加载-VBA

如果数据还没有从bloomberg输出,那么我最终没有足够的数据复制然后粘贴到另一个表单中。

目前,我使用这行代码:Application.OnTime现在+ TIMEVALUE(“零时01分45秒”),“RunAll”

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

有没有其他更有效的方式让我更快地拉入彭博数据,确保数据更快地输出到excel中?

谢谢!

+0

加载单元格是否包含特定的字符串?也许你可以运行一个循环或拦截Worksheet.Change事件来检查这个字符串。 – silentsurfer

+0

请提供示例代码。我不知道彭博数据是什么,所以请提出更通用的问题。你是否使IE自动获取数据?你在建立数据库连接吗? –

+0

你如何检索数据?如果你以编程的方式来做,你将不必像这样等待...... – assylias

回答

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 
+0

我附上了上面的代码的照片。首先,我将bloomberg数据加载到excel中。之后,当人们重新打开文件时,他们可以清除表单内容并刷新表单以提取数据,以防数据发生变化。刷新后,我已经放了一个定时器,等待1分45秒,直到它运行剩下的宏。这是我希望尝试的一种方式,以确保数据以更快的速度刷新和完全输出,但我不知道如何。谢谢! – markos

+0

感谢菲利普,还没有尝试过,因为代码很难理解(我对VBA很新颖)。如果单元格是空白的,它为什么会退出sub?我的问题是,当我刷新我的工作表时,BBRG公式所在的单元格将显示为N/A Requesting Data,有时可能会使用公式加载单元格,但在输出剩余行中的其余信息时滞后在公式所在的单元格下。 – markos

+0

退出sub(1)的单元格只有两种,它所在的单元格是空白的,并且所有数据都被拉入,这意味着没有更多数据,或者(2)您已达到最长时间你愿意等。我检查单元格是否为空,因为上面提到“他们可以清除表单内容并刷新表单以提取数据”。如果他们清除表单,我会认为这意味着它是空的?再次,如果您只需发布您的代码,我可以为您提供更完整的解决方案。 – pheeper