处理它的一种方法是,当你开始你的第二个宏复制数据,检查看看中点单元是否是空的(类似于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
加载单元格是否包含特定的字符串?也许你可以运行一个循环或拦截Worksheet.Change事件来检查这个字符串。 – silentsurfer
请提供示例代码。我不知道彭博数据是什么,所以请提出更通用的问题。你是否使IE自动获取数据?你在建立数据库连接吗? –
你如何检索数据?如果你以编程的方式来做,你将不必像这样等待...... – assylias