Share via

How can I insert the variable correctly to make it work well?

Anonymous
2017-09-14T03:53:54+00:00

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

Microsoft 365 and Office | Excel | For home | Windows

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.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2017-09-14T05:20:27+00:00

Use

... Source = Web.Page(Web.Contents(""" & VA & """))," & Chr(13) & ...

Was this answer helpful?

7 people found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2017-09-14T13:26:10+00:00

    Hi HansV,

    Thank you very much! It works great!

    Lawrence

    Was this answer helpful?

    0 comments No comments