Concatenation of string and looped value

2019-09-12 22:19发布

For Each Number In accountNumber
    Dim urlString As String
    urlString = "http://www.prad.org/CamaDisplay.aspx?OutputMode=Display&SearchType=RealEstate&ParcelID=" & accountNumber.Value
    Set dataSet = ActiveSheet.QueryTables.Add( _
        Connection:="URL;urlString, _
        Destination:=ThisWorkbook.Worksheets(2).Range("A1"))

I am attempting to loop through the values Number in a column range accountNumber, adding them to the end of the URL given. This will ultimately visit multiple webpages, adding to the QueryTables each time.

1条回答
疯言疯语
2楼-- · 2019-09-12 23:01

Answer for length :

From previous question:

Sub FindData()

    Dim accountNumber As Range
    Set accountNumber = Range(Range("A2"), Range("A2").End(xlDown))
    Dim dataSet As QueryTable

    For Each Number In accountNumber
        Set dataSet = ActiveSheet.QueryTables.Add( _
        Connection:="URL;http://www.prad.org/CamaDisplay.aspx?OutputMode=Display&SearchType=RealEstate&ParcelID=" & Number.Value, _
        Destination:=ThisWorkbook.Worksheets(2).Range("A1")


        With dataSet
           .RefreshOnFileOpen = False
           .WebFormatting = xlWebFormattingNone
           .BackgroundQuery = True
           .WebSelectionType = xlSpecifiedTables
           .WebTables = "3"
        End With

        With Application
             dataSet.Refresh BackgroundQuery:=False
        End With
    Next Number
End Sub

You stated you were getting an Invalid Web Query. i don't think you need to append all values to one query. i think you need to run multiple queries, each with one value from the range.

I also think it's not going to be a good idea to have the same destination for all of them.

查看更多
登录 后发表回答