Importing data from a web page to Excel via VBA

Asked

Viewed 5,547 times

5

Here’s the thing: I found the code on the Internet that works perfectly with what you’re indicating, but I need to adapt it to a project and I’m not getting it.

In short, the macro accesses the page http://www.namb.org, checks the member search form, uses the listbox (where it shows the states) as the search parameter (of the various fields it has), returns a table with the requested information and imports it to Excel PLAN1. I would only need to change, instead of using the listbox (States), to use the form’s textbox(First Name). But I can’t do it at all... you could help me?

Follow the code ready:

   Sub extractTablesData()
 'we define the essential variables

 Dim IE As Object, obj As Object
 Dim myState As String
 Dim r As Integer, c As Integer, t As Integer
 Dim elemCollection As Object


 'add the "Microsoft Internet Controls" reference in your VBA Project indirectly
 Set IE = CreateObject("InternetExplorer.Application")

 'more variables for the inputboxes - makes our automation program user friendly

 myState = InputBox("Enter the city where you wish to work")

 With IE

 .Visible = True
 .navigate ("https://www.namb.org")

 ' we ensure that the web page downloads completely before we fill the form automatically
 While IE.ReadyState <> 4
 DoEvents
 Wend

'accessing the ListBox wit States data
For Each obj In IE.Document.All.Item("csSB_Search_State").Options

        If obj.innerText = myState Then

            obj.Selected = True

        End If

    Next obj

 ' accessing the button

 IE.Document.getElementsByName("Search").Item.Click

 ' again ensuring that the web page loads completely before we start scraping data
 Do While IE.busy: DoEvents: Loop

'Clearing any unnecessary or old data in Sheet1
 ThisWorkbook.Sheets("Sheet1").Range("A1:K500").ClearContents

 Set elemCollection = IE.Document.getElementsByTagName("TABLE")

    For t = 0 To (elemCollection.Length - 1)

        For r = 0 To (elemCollection(t).Rows.Length - 1)
            For c = 0 To (elemCollection(t).Rows(r).Cells.Length - 1)
                ThisWorkbook.Worksheets(1).Cells(r + 1, c + 1) = elemCollection(t).Rows(r).Cells(c).innerText
            Next c
        Next r
    Next t

 End With

 ' cleaning up memory
 Set IE = Nothing

 End Sub

I believe I need to make the change in the section below, but I’ve tried (almost) everything and I can’t:

For Each obj In IE.Document.All.Item("csSB_Search_State").Options

        If obj.innerText = myState Then

            obj.Selected = True

        End If

    Next obj

1 answer

4

Just need to replace the block of the for that you scored for this:

IE.document.getelementbyid("csSB_Search_FirstName_ID").Value = myState

I used the same variable MyState not to complicate it, but you can change it if you want.

Probably already this question, but leaves the answer here for future consultations.

  • Thank you so much for your help. Maybe you can help me with this problem: http://stackoverflow.com/questions/34644769/how-to-use-getelementsbytagname-with-td-with-overflow-hidden-on-vba

Browser other questions tagged

You are not signed in. Login or sign up in order to post.