Convert HTML-table to Excel using VBA

后端 未结 9 2087
难免孤独
难免孤独 2021-02-05 02:51

Convert HTML-table to Excel

The code below fetches the HTML-table at https://rasmusrhl.github.io/stuff, and converts it to Excel-format.

The pr

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-05 03:15

    To get the tabular data (keeping the format as it is) from that page, you can try like below:

     Sub Fetch_Data()
        Dim http As New XMLHTTP60, html As New HTMLDocument
        Dim posts As Object, post As Object, elem As Object
        Dim row As Long, col As Long
    
        With http
            .Open "GET", "https://rasmusrhl.github.io/stuff/", False
            .send
            html.body.innerHTML = .responseText
        End With
    
        Set posts = html.getElementsByClassName("gmisc_table")(0)
    
        For Each post In posts.Rows
            For Each elem In post.Cells
                col = col + 1: Cells(row + 1, col).NumberFormat = "@": Cells(row + 1, col) = elem.innerText
            Next elem
            col = 0
            row = row + 1
        Next post
    End Sub
    

    Reference to add to the library:

    1. Microsoft HTML Object Library
    2. Microsoft XML, v6.0  'or whatever version you have
    

    This is how that portion looks like when get parsed.

提交回复
热议问题