A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Use
... Source = Web.Page(Web.Contents(""" & VA & """))," & Chr(13) & ...
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hello,
The following vba code was worked when line 4 replaced with
... ... Source = Web.Page(Web.Contents(""http://www.hkjc.com/chinese/racing/horse.asp?horseno=T232""))," & Chr(13) & ... ...
However, in order to do it more effectively and automatically, I want to replace a variable for website address, as below:
... ... Source = Web.Page(Web.Contents(" & VA & "))," & Chr(13) & ... ...
And I don't know why, it doesn't work to scrape anything. How can I insert the variable correctly to make it work well?
Thank you very much and hopefully somebody could help.
Lawrence
-----Original VBA code-----
sub ScrapeWebData
VA = Sheets(1).Range("AB1").Value
ActiveWorkbook.Queries.Add Name:="Table 0", Formula:= _
"let" & Chr(13) & "" & Chr(10) & " Source = Web.Page(Web.Contents(" & VA & "))," & Chr(13) & "" & Chr(10) & " Data0 = Source{0}[Data]," & Chr(13) & "" & Chr(10) & " #""Changed Type"" = Table.TransformColumnTypes(Data0,{{""Column1"", type text}, {""Column2"", type text}, {""Column3"", type text}})" & Chr(13) & "" & Chr(10) & "in" & Chr(13) & "" & Chr(10) & " #""Changed Type"""
Sheets.Add after:=ActiveSheet
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=""Table 0""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Table 0]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = False
.ListObject.DisplayName = "Table_0"
.Refresh BackgroundQuery:=False
End With
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
end sub
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.
Answer accepted by question author
Use
... Source = Web.Page(Web.Contents(""" & VA & """))," & Chr(13) & ...
Hi HansV,
Thank you very much! It works great!
Lawrence