ExcelSammy
New Member
- Joined
- Jun 13, 2019
- Messages
- 3
Hello! Someone please help!
I think my issue is most likely a syntax issue but I need to pass parameters to yahoo finance to scrape the data for different tickers on different exchanges. To simplify my problem i just created a seperate test Sub to show my problem. I created 2 string variables (ticker, exchange) and set to examples "TGIF" and "CN" (these are valid) now i just want to ammend these variables as part of the URL.
the problem is it is not connecting to the table on the website i am trying to scrape. If i hard code the symbol and exchange as part of the URL it works. I am so confused, please help!!
I think my issue is most likely a syntax issue but I need to pass parameters to yahoo finance to scrape the data for different tickers on different exchanges. To simplify my problem i just created a seperate test Sub to show my problem. I created 2 string variables (ticker, exchange) and set to examples "TGIF" and "CN" (these are valid) now i just want to ammend these variables as part of the URL.
Code:
Sub OpenWebStockDataTest()
'
' OpenWebStockDataTest Macro
'
'
Dim sticker As String
Dim exchange As String
sticker = "TGIF"
exchange = "CN"
ActiveWorkbook.Queries.Add Name:="Table 2", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(""https://finance.yahoo.com/quote/"" & sticker & ""."" & exchange &""/history?p="" &sticker &"".""&exchange))," & Chr(13) & "" & Chr(10) & " Data2 = Source{2}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data2,{{""Date"", type date}, {""Open"", type number}, {""High"", type number}, {""Low"", type number}, {""Close*"", type number}, {""Adj Close**"", type number}, {""Volume"", Int64" & _
".Type}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
ActiveWorkbook.Worksheets.Add
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 2"";Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 2]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_2"
.Refresh BackgroundQuery:=False
End With
End Sub
the problem is it is not connecting to the table on the website i am trying to scrape. If i hard code the symbol and exchange as part of the URL it works. I am so confused, please help!!