简体   繁体   English

调试QueryTables.Add脚本

[英]Debugging a QueryTables.Add script

Sub FindData()

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

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

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

With Application
    dataSet.Refresh BackgroundQuery:=False
End With

End Sub

The ultimate goal here is to pull data from the URL and drop it into Worksheet(2) . 这里的最终目标是从URL提取数据并将其放入Worksheet(2) The values in accountNumber go at the end of the URL for each page to draw data from. accountNumber的值位于每个页面的URL末尾,以从中提取数据。

This is my first VBA script, and right off the bat, it's giving me an error on Sub FindData() 这是我的第一个VBA脚本,很快,它给我Sub FindData()一个错误。

I have the table of accountNumbers. 我有一个accountNumbers表。 The URL for one account is the given URL with an accountNumber after the final =. 一个帐户的URL是给定的URL,在final =后带有一个accountNumber。 I am trying to iterate through one webpage per accountNumber and extract from each. 我正在尝试遍历每个accountNumber一个网页并从每个网页中提取。

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

QueryTables needs to be properly referenced. QueryTables需要正确引用。 You can use a sheet qualifier like : Sheets("yourname").QueryTables or something. 您可以使用工作表限定符,例如:Sheets(“ yourname”)。QueryTables之类的东西。 You can remove the dot too... 您也可以删除圆点...

Look into my code and see if this helps. 查看我的代码,看看是否有帮助。 I added a lot of comments to help you understand better the way the whole thing works. 我添加了很多评论,以帮助您更好地了解整个过程。

Option Explicit

Sub FindData()

    Const strURL As String = "URL;http://www.prad.org/CamaDisplay.aspx?OutputMode=Display&SearchType=RealEstate&ParcelID="

    Dim shActive As Worksheet
    Dim shDestination As Worksheet
    Dim oQuery As QueryTable
    Dim rAccounts As Range
    Dim rAccount As Range


    'Initialize the variables
    Set shActive = ActiveSheet

    ' Note the "." in front of the ranges. That's how you use "With"
    With shActive
        Set rAccounts = .Range(.Range("A2"), .Range("A2").End(xlDown))
    End With

    ' Remove any old query otherwise they will pile up and slow down
    ' your workbook
    Call RemoveSheetQueries(shActive)


    ' Loop through the accounts and add the queries
    For Each rAccount In rAccounts
        Set oQuery = Nothing
        Set oQuery = shActive.QueryTables.Add(Connection:=strURL & rAccount.Value, _
                                              Destination:=shActive.Range("A1"))

        ' Set the properties of the new query and eventually run it.
        With oQuery
            .RefreshOnFileOpen = False
            .WebFormatting = xlWebFormattingNone
            .BackgroundQuery = True
            .WebSelectionType = xlSpecifiedTables
            .WebTables = "3"

            ' This last line will actually get the data
            .Refresh BackgroundQuery:=False
        End With
    Next rAccount

End Sub



' Procedure to remove all old Queries
Sub RemoveSheetQueries(ByRef shToProcess As Worksheet)

    Dim lTotal As Long
    Dim i As Long

    lTotal = shToProcess.QueryTables.Count

    For i = lTotal To 1 Step -1
        shToProcess.QueryTables(i).Delete
    Next i

End Sub

I hope it helps :) 希望对您有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM