Hi!
I've come up with a VBA that works well to download CSV chart from Morningstar and Yahoo Finance for Canadian stock. However, it always gives me a "1004 run time error" (which points on .Refresh BackgroundQuery:=False) for stocks that contain hyphens or dots i.e. like ATD-B.TO (ATD.B) or RCI-B.TO (RCI.B). All help is greatly appreciated!
VBA CODE FOR KEY RATIOS (from Morningstar)
VBA CODE FOR PRICES (from yahoo finances)
I've come up with a VBA that works well to download CSV chart from Morningstar and Yahoo Finance for Canadian stock. However, it always gives me a "1004 run time error" (which points on .Refresh BackgroundQuery:=False) for stocks that contain hyphens or dots i.e. like ATD-B.TO (ATD.B) or RCI-B.TO (RCI.B). All help is greatly appreciated!
VBA CODE FOR KEY RATIOS (from Morningstar)
Code:
Sub IMPORT_KeyRatios()
Dim ConnString As String
Dim Cell As Range
Dim Ticker As String, Pays As String, Bourse As String
Sheets("Vierge").Activate
Ticker = Sheets("Vierge").Cells(13, "G").Value
Pays = Sheets("Vierge").Cells(6, "G").Value
Bourse = Sheets("Vierge").Cells(5, "G").Value
ConnString = "TEXT;http://financials.morningstar.com/ajax/exportKR2CSV.html?&callback=?&t=X" + Bourse + ":" + Ticker + "®ion=" + Pays + "&culture=en-US&cur=&order=asc"
Sheets.Add.Name = "KeyRatios"
With ActiveSheet.QueryTables.Add(Connection:=ConnString, Destination:=Range("A1"))
.Name = _
" "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileDecimalSeparator = ","
.Refresh BackgroundQuery:=False
End With
End Sub
VBA CODE FOR PRICES (from yahoo finances)
Code:
Sub IMPORT_Prices()
Dim ConnString As String
Dim TEXT As String
Dim StartDay As String
Dim StartMonth As String
Dim StartYear As String
Dim EndDay As String
Dim EndMonth As String
Dim EndYear As String
Dim Ticker As String
Dim Cell As Range
Sheets("Vierge").Activate
Ticker = Sheets("Vierge").Cells(13, "G").Value
StartMonth = Sheets("Vierge").Cells(8, "G").Value
StartDay = Sheets("Vierge").Cells(8, "H").Value
StartYear = Sheets("Vierge").Cells(8, "I").Value
EndMonth = Sheets("Vierge").Cells(9, "G").Value
EndDay = Sheets("Vierge").Cells(9, "H").Value
EndYear = Sheets("Vierge").Cells(9, "I").Value
ConnString = "TEXT;http://real-chart.finance.yahoo.com/table.csv?s=" + Ticker + "&a=" + StartMonth + " &b=" + StartDay + " &c=" + StartYear + "&d=" + EndMonth + "&e=" + EndDay + "&f=" + EndYear + "&g=m&ignore=.csv"
Sheets.Add.Name = "Prices"
With ActiveSheet.QueryTables.Add(Connection:=ConnString, Destination:=Range("A1"))
.Name = _
" "
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.TextFilePromptOnRefresh = False
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileDecimalSeparator = ","
.Refresh BackgroundQuery:=False
End With
Sheets("Prices").Activate
Range("B1").EntireColumn.Delete
Range("D1:F1").EntireColumn.Delete
End Sub