Excel VBA - 从网站复制表格数据并放入Excel?

时间:2017-12-07 12:16:21

标签: excel vba excel-vba internet-explorer

我正在尝试从网站复制表格并将其放入Excel中。 我的表可以通过elementID“VisibleReportContentctl32”来识别。

由于某种原因,我的代码产生了这个错误: '需要对象'

此行上的

:范围(“A1”)。文本= dat

这是我的完整代码:

Option Explicit
Public Sub GetSSRSData()

    Dim IE            As Object: Set IE = New InternetExplorerMedium
    Dim TR_Elements   As Object
    Dim TR            As Object ' Table Row
    Dim TD_Elements   As Object
    Dim TD            As Object ' Table Data
    Dim RowNumb       As Integer
    Dim Columns       As Integer
    Dim ColumnNumb    As Integer
    Dim x As Integer
    Dim dat As String

    With IE
        .Visible = True
        .Navigate ("http://gbrlon02-sql-17/Reports/Pages/Report.aspx?ItemPath=%2fCutlass+Reports%2fManagement+Reporting%2fForwardOrdersSticksOnly+-+ForecastVariance+(Monthly+Report)")

         Do While IE.Busy Or IE.ReadyState <> 4
             DoEvents
         Loop

         If IsObject(.Document.getElementById("VisibleReportContentctl32_ctl09")) Then
            dat = IE.Document.getElementById("VisibleReportContentctl32_ctl09").innerHTML
            Range("A1").Text = dat
         Else
            MsgBox "doesn't"
         End If
   End With
End Sub

请有人告诉我我哪里出错了吗?

2 个答案:

答案 0 :(得分:0)

使用[Power Query] [来自网络的表格]简单而稳定

答案 1 :(得分:0)

该网址对我不起作用。你的代码看起来应该是这样......

Option Explicit 
Sub gethtmltable() 
    Dim objWeb As QueryTable 
    Dim sWebTable As String 
     'You have to count down the tables on the URL listed in your query
     'This example shows how to retrieve the 2nd table from the web page.
    sWebTable = 2 
     'Sets the url to run the query and the destination in the excel file
     'You can change both to suit your needs
    Set objWeb = ActiveSheet.QueryTables.Add( _ 
    Connection:="URL;http://www.vbaexpress.com/kb/default.php", _ 
    Destination:=Range("A1")) 

    With objWeb 

        .WebSelectionType = xlSpecifiedTables 
        .WebTables = sWebTable 
        .Refresh BackgroundQuery:=False 
        .SaveData = True 
    End With 
    Set objWeb = Nothing 
End Sub 

您只需要确定要从中导入数据的特定表。