Gasman
Enthusiastic Amateur
- Local time
- Today, 21:21
- Joined
- Sep 21, 2011
- Messages
- 16,607
Hi all,
I have an Excel sheet that used to download a few stock quotes. Yahoo have now stopped access.
So I am tring another method, this time using Google finance.
From a few YouTube videos I have pieced together the code below.
If I take the connection string and paste manually (which is how I recorded the macro) it works. it also works if the ticker is hardcoded in the connection string.
However soon as I try and parameterise the string it says it cannot find the file.?
debug of string is
TEXT;http://finance.google.co.uk/finance/historical?q=LON:SSE&output=csv
I have an Excel sheet that used to download a few stock quotes. Yahoo have now stopped access.
So I am tring another method, this time using Google finance.
From a few YouTube videos I have pieced together the code below.
If I take the connection string and paste manually (which is how I recorded the macro) it works. it also works if the ticker is hardcoded in the connection string.
However soon as I try and parameterise the string it says it cannot find the file.?
debug of string is
TEXT;http://finance.google.co.uk/finance/historical?q=LON:SSE&output=csv
Code:
Sub GetStockPrices()
Dim rngTicker As Range, rngRow As Range
Dim strTicker As String, strPrice As String
Call RemoveConnections
Sheets("Output").Select
Set rngTicker = Range("A2:A3")
For Each rngRow In rngTicker
strTicker = Range("A" & rngRow.row).Value
Sheets("Data").Select
Cells.Delete
Call Macro3(strTicker)
'Do
DoEvents
strPrice = Sheets("Data").Range("B2").Value
Range("B" & rngRow.row).Value = strPrice
Next rngRow
Set rngTicker = Nothing
Set rngRow = Nothing
End Sub
Sub Macro3(pstrTicker As String)
'
' Macro3 Macro
Dim strConnection
'strConnection = "TEXT;http://finance.google.co.uk/finance/historical?q=LON:" & pstrTicker & "&startdate=Nov+7,+2017&enddate=Nov+7,+2017&num=30&ei=vAcDWsCoMYeEU6fDgdgJ&output=csv"
strConnection = "TEXT;http://finance.google.co.uk/finance/historical?q=LON:" & pstrTicker & "&output=csv"
Debug.Print strConnection
Sheets("Data").Select
Cells.Clear
With ActiveSheet.QueryTables.Add(Connection:=strConnection, _
Destination:=Sheets("Data").Range("$A$1"))
.Name = ""
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 65001
.TextFileStartRow = 2
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With
End Sub
Sub RemoveConnections()
Dim xConnect As Object
For Each xConnect In ActiveWorkbook.Connections
xConnect.Delete
Next xConnect
Set xConnect = Nothing
End Sub