Trying to fetch data from webpage with a VBA code, but sometimes it works and sometimes it does not fetch

后端 未结 1 809
情书的邮戳
情书的邮戳 2021-01-28 07:11

I have collected this vba code from a websites. it should fetch data from a webpage. But sometimes if I write value for what and where it fetches data accordingly, sometimes it

相关标签:
1条回答
  • 2021-01-28 08:06

    Here you go.

    Example
    Type of Job : Accountant
    zipcode :94551

    Sub GetData()
    
        Dim eRow As Long
        Dim html As Object, ele As Object, xmlHttp As Object
        Dim URL As String, myjobtype As String, myzip As String
    
        Set sht = Sheets("Sheet1")
        RowCount = 1
        sht.Range("A" & RowCount) = "Title"
        sht.Range("B" & RowCount) = "Company"
        sht.Range("C" & RowCount) = "Location"
        sht.Range("D" & RowCount) = "Description"
    
        eRow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    
        myjobtype = InputBox("Enter type of job eg. sales, administration")
        myzip = InputBox("Enter zipcode of area where you wish to work")
    
    
        Set xmlHttp = CreateObject("MSXML2.XMLHTTP")
    
        URL = "http://jobs.com/search?where=" & myzip & "&q=" & myjobtype & "&rnd=" & WorksheetFunction.RandBetween(1, 1000)
    
        xmlHttp.Open "GET", URL, False
        xmlHttp.setRequestHeader "Content-Type", "text/xml"
        xmlHttp.send
    
    
    
        Set html = CreateObject("htmlfile")
        html.body.innerHTML = xmlHttp.ResponseText
    
        For Each ele In html.all
            Select Case ele.classname
            Case "Result"
                RowCount = RowCount + 1
            Case "Title"
                sht.Range("A" & RowCount) = ele.innertext
            Case "Company"
                sht.Range("B" & RowCount) = ele.innertext
            Case "Location"
                sht.Range("C" & RowCount) = ele.innertext
            Case "Description"
                sht.Range("D" & RowCount) = ele.innertext
            End Select
        Next ele
    
        Macro1
    End Sub
    
    Sub Macro1()
    '
    ' Macro1 Macro
    ' Formatting imported data
    '
    '
        Columns("A:D").Select
        Selection.Columns.AutoFit
        With Selection
            .VerticalAlignment = xlTop
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
        End With
        Range("D1").Select
        Columns("D:D").ColumnWidth = 50
        Columns("A:D").Select
        Selection.Rows.AutoFit
    End Sub
    
    0 讨论(0)
提交回复
热议问题