Hello excer'rs. I'm well through my current macro, but hung up on I guess what you'd call error handling. The code below loops through web addresses that contain a date 30x, downloading each days' data with a web query off of its web page. It seems that a couple things generate an error, server too busy possibly (and other error sources out of my control or knowledge) as well as no data/ page existing at all for some of the dates, like Sunday. I need to know how to not only resume execution when I hit an error but immediately jump to the incrementing of the loop counter to avoid running the loop's code that follows the web query part. Hope that makes sense. Any help anyone could offer would be greatly appreciated...
Code:
Sub EarningsMacro()
'The sub that downloads earnings dates.
'Looks out one month ahead.
'Dates can be added to the data on the website within a month, so it has to check for the whole month every day.
'Need variables TodayMonth, TodayDay, TodayYear, CheckMonth, CheckDay, CheckYear
Dim NextDay As Date 'The variable that holds the date being checked
Dim NextDayString As String 'The web address that holds the variable to change the date. Passed to the query in the loop.
Dim EAQ As Worksheet 'Earnings worksheet query
Dim EA As Worksheet 'Earnings worksheet
Dim i As Integer 'loop counter
Dim EAQFinalrow As Long 'The final row of the earings query sheet
Dim QT As QueryTable 'The query table
Dim EANewRow As Long 'The new row of the earnings main worksheet
Dim EarningsRangeSize As Long 'The size of the new data block (# of rows)
Set EAQ = Worksheets("EarningsQuery") 'Abbreviate/ set worksheet reference.
Set EA = Worksheets("Earnings") 'Abbreviate/ set worksheet reference.
For i = 1 To 30 'Loop counter, get each date and data with each pass.
'Delete the old web query.
EAQFinalrow = FinalRowFunct("EarningsQuery", 1)
EAQ.Range(EAQ.Cells(1, 1), EAQ.Cells(EAQFinalrow, 1)).EntireRow.Delete
'Format desired (needed for string address is Format("yyyymmdd")
NextDay = Date + i
'Yahoo address: [URL]http://biz.yahoo.com/research/earncal/20090320.html[/URL] (includes date of March 20, 2009)
NextDayString = "[URL]http://biz.yahoo.com/research/earncal/[/URL]" & Format(NextDay, "yyyymmdd") & ".html"
'Define a new web query
Set QT = EAQ.QueryTables.Add(Connection:="URL;" & NextDayString, Destination:=EAQ.Range("$A$1"))
With QT
.Name = "20090320"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "5"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With
'Refresh the query
QT.Refresh BackgroundQuery:=False
'Find the final row of the query worksheet
'EAQFinalrow = EAQ.Cells(Rows.count, 1).End(xlUp).Row
EAQFinalrow = FinalRowFunct("EarningsQuery", 1)
'Find the length of the data range
EarningsRangeSize = EAQFinalrow - 4
'Get the new row of the earnings main worksheet
EANewRow = FinalRowFunct("Earnings", 3) + 1
'Copy the company column to the earnings main worksheet
EAQ.Range(EAQ.Cells(4, 1), EAQ.Cells(EAQFinalrow, 1)).Copy EA.Cells(EANewRow, 4)
'Copy the symbol column to the earnings main worksheet
EAQ.Range(EAQ.Cells(4, 2), EAQ.Cells(EAQFinalrow, 2)).Copy EA.Cells(EANewRow, 3)
'Copy the time column to the earnings main worksheet
EAQ.Range(EAQ.Cells(4, 4), EAQ.Cells(EAQFinalrow, 4)).Copy EA.Cells(EANewRow, 7)
'Enter the earnings date column to the earnings main worksheet
EA.Range(EA.Cells(EANewRow, 6), EA.Cells(EANewRow + EarningsRangeSize, 6)).Value = Format(NextDay, "mm/dd/yyyy")
'Call sub. Enter "NA" for the 3rd, 4th and 5th criteria columns
AddNA "Earnings", 3, 8, 10
Next i
End Sub