The code works great, but it does lack one MAJOR item... ERROR Checking for Stock Symbols that are mistyped like "MTE" instead of "MET". Clearing the cells and then no data from Yahoo Stock History causes issues with the formulas in columns H"AF. I am trying to stop the process of clearing cells, if there is an error from Yahoo.
Below is the portion of code that is in question.
No matter where I insert "'On Error GoTo StockSymbolEntryError1" code, it goes directly to 'StockSymbolEntryError1: Which is a MsgBox that displays which error msg it is. if I put it before the Yahoo code to down load the Stock history data I still it still sees an error. IF a correct Stock Symbol is entered, it still goes to the error GoTo. Is there a specific error code from Yahoo that could be used? Sorry about all the Remarks in the code as that is how I keep track as to what is going on...
Thanks in advance for your assistance
Don
Below is the portion of code that is in question.
No matter where I insert "'On Error GoTo StockSymbolEntryError1" code, it goes directly to 'StockSymbolEntryError1: Which is a MsgBox that displays which error msg it is. if I put it before the Yahoo code to down load the Stock history data I still it still sees an error. IF a correct Stock Symbol is entered, it still goes to the error GoTo. Is there a specific error code from Yahoo that could be used? Sorry about all the Remarks in the code as that is how I keep track as to what is going on...
Thanks in advance for your assistance
Don
Code:
'-------------------------------------------------------
[COLOR=#008000] ' Clear all cells in columns A:G formula rows from H5 through AF last row
'-------------------------------------------------------[/COLOR]
Worksheets("BackTest").Range("a:g").ClearContents
With Worksheets("BackTest")
LastRow1 = .Range("H" & .Rows.Count).End(xlUp).Row
.Range("H6:af" & LastRow1).Clear
End With
[COLOR=#008000]'------------------------------------------------------
' Get Yahoo Stock History Data
'------------------------------------------------------[/COLOR]
Set SettingsSheet = Workbooks("BackTest From Scratch - 13.xlsm").Worksheets("Setup")
SYMBOL = SettingsSheet.Range("D5")
StartDate = SettingsSheet.Range("D6").Value
EndDate = SettingsSheet.Range("D7").Value
qurl = "http://ichart.finance.yahoo.com/table.csv?s=" & SYMBOL
qurl = qurl & "&a=" & Month(StartDate) + 1 & "&b=" & Day(StartDate) & _
"&c=" & Year(StartDate) - 1 & "&d=" & Month(EndDate) - 1 & "&e=" & _
Day(EndDate) & "&f=" & Year(EndDate) & "&g=d&q=q&y=0&z=" & _
SYMBOL & "&x=.csv"
[COLOR=#008000] '--------------------------------------------
' Check for valid stock symbol
'On Error GoTo StockSymbolEntryError1
'--------------------------------------------[/COLOR]
QueryQuote:
Set QuerySheet = Workbooks("BackTest From Scratch - 13.xlsm").Worksheets("BackTest")
With QuerySheet.QueryTables.Add(Connection:="URL;" & qurl, Destination:=QuerySheet.Range("a1"))
.BackgroundQuery = True
.TablesOnlyFromHTML = False
[COLOR=#008000] '--------------------------------------------
' Original Code - Error Check and Location
' >>>>>>>> On Error Resume Next <<<<<<<<
'--------------------------------------------
'--------------------------------------------
'Check for valid stock symbol
' On Error GoTo StockSymbolEntryError2
'--------------------------------------------[/COLOR]
.Refresh BackgroundQuery:=False
.SaveData = True
[COLOR=#008000] '--------------------------------------------------
[/COLOR][COLOR=#ff0000]'Should the clearing of the A:G cells be entered here? IF there I no ERROR condition should the H:AF &
' LastRow -4 go here?
[/COLOR][COLOR=#008000] ' --------------------------------------------------[/COLOR]
Sheets("BackTest").Range("a1").CurrentRegion.TextToColumns Destination:=Sheets("BackTest").Range("a1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, other:=False
End With
Sheets("BackTest").Columns("A:ah").AutoFit
[COLOR=#008000] '---------------------------------------------------------
'Lastrow and Countrows of new data download
'---------------------------------------------------------[/COLOR]
With Worksheets("BackTest")
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
MsgBox "LastRow: " & LastRow
End With
[COLOR=#008000]'--------------------------------------------------------
' Clear data from H6 through AH & LastRow
'--------------------------------------------------------
'With Worksheets("BackTest")
'.Range("H6:ae" & LastRow).Clear
'LastRow2 = .Range("H" & .Rows.Count).End(xlUp).Row
'MsgBox "LastRow2: " & LastRow2
'End With
'--------------------------------------------------------[/COLOR]
[COLOR=#008000] ' Drag down formulas from H5 through LastRow -4
'--------------------------------------------------------[/COLOR]
With Worksheets("BackTest")
.Range("h5:af5").AutoFill Destination:=.Range("h5:af" & LastRow - 5)
'LastRow3 = .Range("H" & .Rows.Count).End(xlUp).Row
'MsgBox "LastRow3: " & LastRow3
End With