處理它的一種方法是,當你開始你的第二個宏複製數據,檢查看看中點單元是否是空的(類似於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