Import tables from a webpage to excel

后端 未结 1 1871
刺人心
刺人心 2021-01-28 03:35

I just have an upper-intermediate level of excel and intermediate background of VBA with excel. What I want to do is to import tables from a webpage like this which appears in t

1条回答
  •  滥情空心
    2021-01-28 03:56

    This code get the data from all the links.

    Sub Extract_data()
    
        Dim url As String, links_count As Integer
        Dim i As Integer, j As Integer, row As Integer
        Dim XMLHTTP As Object, html As Object
        Dim tr_coll As Object, tr As Object
        Dim td_coll As Object, td As Object
    
        links_count = 39
        For i = 0 To links_count
    
            url = "http://www.admision.unmsm.edu.pe/res20130914/A/011/" & i & ".html"
    
            Set XMLHTTP = CreateObject("MSXML2.XMLHTTP")
            XMLHTTP.Open "GET", url, False
            XMLHTTP.send
    
            Set html = CreateObject("htmlfile")
            html.body.innerHTML = XMLHTTP.ResponseText
    
            Set tbl = html.getelementsbytagname("Table")
    
            Set tr_coll = tbl(0).getelementsbytagname("TR")
    
            For Each tr In tr_coll
                j = 1
                Set td_col = tr.getelementsbytagname("TD")
    
                For Each td In td_col
                    Cells(row + 1, j).Value = td.innerText
                    j = j + 1
                Next
                row = row + 1
            Next
        Next
    
        MsgBox "Done"
    End Sub
    

    0 讨论(0)
提交回复
热议问题