VBA xmlhttp GET - 从具有不规则结构的表中获取数据 [英] VBA xmlhttp GET - getting data from table with irregular structure

查看:31
本文介绍了VBA xmlhttp GET - 从具有不规则结构的表中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过 xmlhttp GET 从网站获取数据.不幸的是,表格在一行或一列中的行中没有恒定数量的列,因为有些单元格被合并了(我什至不得不在宏中手动将最大列数更改为 11,因为第一行的列数较少).

I try to get data from website through xmlhttp GET. Unfortunately table doesn't have a constant amount of columns in a row or rows in a column, because some cells are merged (I even had to change max amount of columns manually to 11 in the macro as 1st row has fewer columns).

我希望输出与网站上的完全一样.

I would like the output to go exactly as on the website.

Option Explicit

Public Sub GetTable()

Dim oDom As Object: Set oDom = CreateObject("htmlFile")
Dim x As Long, y As Long
Dim oRow As Object, oCell As Object
Dim vData As Variant
Dim link As String

link = "http://medicarestatistics.humanservices.gov.au/statistics/do.jsp?_PROGRAM=%2Fstatistics%2Fmbs_group_standard_report&DRILL=on&GROUP=Broad+Type+of+Service+%28BTOS%29&VAR=services&STAT=count&RPT_FMT=by+time+period+and+state&PTYPE=month&START_DT=201609&END_DT=201609"

y = 1: x = 1

With CreateObject("msxml2.xmlhttp")
    .Open "GET", link, False
    .Send
    oDom.body.innerHtml = .responseText
End With

With oDom.getelementsbytagname("table")(0)
    ReDim vData(1 To .Rows.Length, 1 To 11) '.Rows(1).Cells.Length)
    For Each oRow In .Rows
        For Each oCell In oRow.Cells
            vData(x, y) = oCell.innerText
            y = y + 1
        Next oCell
        y = 1
        x = x + 1
    Next oRow
End With

Sheets(1).Cells(1, 1).Resize(UBound(vData), UBound(vData, 2)).Value = vData
End Sub

推荐答案

只需在每次循环时检查行长度,如果需要更多列,请调整数组大小:

Just check the row length each time through your loop and resize the array if you need more columns:

With oDom.getelementsbytagname("table")(0)
    Dim rowCount As Long
    rowCount = .Rows.Length
    ReDim vData(1 To rowCount, 1 To .Rows(0).Cells.Length)
    For Each oRow In .Rows
        Dim columnCount As Long
        columnCount = .Rows(x - 1).Cells.Length
        If columnCount > UBound(vData, 2) Then
            ReDim Preserve vData(1 To rowCount, 1 To columnCount)
        End If
        For Each oCell In oRow.Cells
            vData(x, y) = oCell.innerText
            y = y + 1
        Next oCell
        y = 1
        x = x + 1
    Next oRow
End With

没有检查源表中的列跨度.一种选择是使用@Thunderframe 的建议并测试所有列跨度,但这似乎有点乏味.我个人会利用 Excel 知道如何从剪贴板粘贴 HTML 的事实,让 Excel 弄清楚:

Didn't check the column spans in the source table. One option would be to use @Thunderframe's suggestion and test for all of the column spans, but that seems a bit tedious. I'd personally leverage the fact that Excel knows how to paste HTML from the clipboard, and just let Excel figure it out:

With oDom.getelementsbytagname("table")(0)
    Dim dataObj As Object
    Set dataObj = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    dataObj.SetText "<table>" & .innerHtml & "</table>"
    dataObj.PutInClipboard
End With

Sheets(1).Paste Sheets(1).Cells(1, 1)

这篇关于VBA xmlhttp GET - 从具有不规则结构的表中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆