+ Reply to Thread
Results 1 to 3 of 3

[SOLVED] Macro that grabs data from table

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2015
    Location
    Atlanta GA
    MS-Off Ver
    2007
    Posts
    16

    [SOLVED] Macro that grabs data from table

    I am trying to grab the Item specific data (color, model, mileage, year) from a group of links pasted in the A column (example http://www.ebay.com/itm/Harley-David...m=181746283957), and place the data in subsequent columns. This is what I have so far, but I get an error box "438: Object doesn't support this property or method". I have no idea what this means and what is wrong with my code?? Thanks!!
    Sub LinkGrab()
    Dim Ie As New InternetExplorer
    Dim WebURL
    Dim Docx As HTMLDocument
    Dim Make
    Dim Model
    Dim Mileage
    Dim vin
    Dim Year
    Dim Esize
    Dim Color
    
    Ie.Visible = False
    
    For RcdNum = 2 To ThisWorkbook.Worksheets(1).Range(ThisRange).End(xlUp).Row
    
    WebURL = ThisWorkbook.Worksheets(1).Range("A" & RcdNum)
    Ie.Navigate2 WebURL
    Do Until Ie.readyState = READYSTATE_COMPLETE
    DoEvents
    Loop
    Set Docx = Ie.document
    Set Container = Docx.getElementsById("readMoreDesc")
    Info = Container.Children
    If Info.className = "attrLabels" Then
        If Info.attrLabels = "VIN (Vehicle Identification Number)" Then
          vin = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Year" Then
          Year = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Make" Then
          Make = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Engine Size (cc)" Then
          Esize = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Model" Then
          Model = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Mileage" Then
          Mileage = Info.attrLabels.getElementsByTagName("span")(0).innerText
        ElseIf Info.attrLabels = "Color" Then
          Color = Info.attrLabels.getElementsByTagName("span")(0).innerText
        End If
    End If
        
          
         
        
    
    ThisWorkbook.Worksheets(1).Range("B" & RcdNum) = Year
    ThisWorkbook.Worksheets(1).Range("C" & RcdNum) = Color
    ThisWorkbook.Worksheets(1).Range("D" & RcdNum) = Make
    ThisWorkbook.Worksheets(1).Range("E" & RcdNum) = Model
    ThisWorkbook.Worksheets(1).Range("F" & RcdNum) = Mileage
    ThisWorkbook.Worksheets(1).Range("G" & RcdNum) = vin
    ThisWorkbook.Worksheets(1).Range("H" & RcdNum) = Esize
    Next
    
    End Sub
    Last edited by gautum123; 05-22-2015 at 11:53 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Macro that grabs data from table

    Hello gautum123,

    Welcome to the Forum!

    The attached workbook walks down the URLs in column "A" starting with "A2" and going down until an empty cell is found. The data from the table is out in the same row as the URL.

    There is a button in row 1 to call the macro. This uses a faster and more reliable method of retrieving data. Rather than using Internet Explorer, this macro uses the engine for Internet explorer to access the HTML data. There are actually three macros. The main macro is "GetData". This calls two other macros. First it calls "GetWebDocument" whichs creates an HTML DOM object from the web page's source text. The last macro "GetElemText" is called to parse the text from an HTML element recursively. This allows retrieving a full table row of data with a single call. All text elements are returned as a single string. Each "TD" element in the table is followed by a pipe character "|" to allow the string to split into the appropriate columns.

    Module1 Macro Code
    Global HTMLdoc As Object
    
    Function GetElemText(ByRef Elem As Object, Optional ByRef ElemText As String) As String
    
      ' Written:  March 18, 2015
      ' Author:   Leith Ross
      '
      ' This is a recursive procedure to extract text from between
      ' an element's start tag and end tag and everything in between.
        
        If Elem Is Nothing Then ElemText = "~": Exit Function
        
          ' Is this element a text value?
            If Elem.NodeType = 3 Then
              ' Separate text elements with a space character.
                ElemText = ElemText & Elem.NodeValue & " "
            Else
              ' Keep parsing - Element contains other non text elements.
                For Each Elem In Elem.ChildNodes
                    Select Case UCase(Elem.NodeName)
                        Case Is = "BR": ElemText = vbLf
                        Case Is = "TD": If ElemText <> "" Then ElemText = ElemText & "|"
                        Case Is = "TR": ElemText = ElemText & vbLf
                    End Select
                    Call GetElemText(Elem, ElemText)
                Next Elem
            End If
            
        GetElemText = ElemText
        
    End Function
    
    Function GetWebDocument(ByVal URL As String) As Variant
    
        Dim Text As String
        
            Set HTMLdoc = Nothing
                
            With CreateObject("MSXML2.XMLHTTP")
                .Open "GET", URL, True
                .Send
                
                While .readyState <> 4: DoEvents: Wend
                
                If .Status <> 200 Then
                    GetWebDocument = "ERROR:  " & .Status & " - " & .StatusResponse
                    Exit Function
                End If
                
                Text = .responseText
            End With
            
            Set HTMLdoc = CreateObject("htmlfile")
            HTMLdoc.Write Text
            HTMLdoc.Close
            
    End Function
    
    Sub GetData()
    
        Dim Data    As Variant
        Dim n       As Long
        Dim oDiv    As Object
        Dim oTable  As Object
        Dim ret     As Variant
        Dim Rng     As Range
        Dim Text    As String
        
        
            Set Rng = Range("A2")
            
            Do While Not IsEmpty(Rng)
                ret = GetWebDocument(Rng)
        
              ' Check for a web page error.
                If Not IsEmpty(ret) Then
                    Rng.Offset(0, 1).Value = ret
                    GoTo NextURL
                End If
            
                Set oDiv = HTMLdoc.getElementByID("vi-desc-maincntr")
            
                  ' Locate the Item Specifics Table.
                    For n = 0 To oDiv.Children.Length - 1
                        If oDiv.Children(n).NodeType = 1 Then
                            If oDiv.Children(n).className = "itemAttr" Then
                                On Error Resume Next
                                    Set oDiv = oDiv.Children(n)
                                    Set oDiv = oDiv.Children(0)
                                    Set oTable = oDiv.Children(2)
                                On Error GoTo 0
                                Exit For
                            End If
                        End If
                    Next n
                
                  ' Check if Table exists.
                    If oTable Is Nothing Then
                        Rng.Offset(0, 1).Value = "Item Specifics were not found on this page."
                        GoTo NextURL
                    End If
                
                    c = 1
                
                  ' Read the row data and output it to the worksheet.
                    For n = 0 To oTable.Rows.Length - 1
                        Text = ""
                        Text = GetElemText(oTable.Rows(n), Text)
                        
                      ' To avoid an error, check there is text to output.
                        If Text <> "" Then
                            Data = Split(Text, "|")
                            Rng.Offset(0, c).Resize(1, UBound(Data) + 1).Value = Data
                            c = c + UBound(Data) + 1
                        End If
                    Next n
                    
    NextURL:
                Set Rng = Rng.Offset(1, 0)
            Loop
                
    End Sub
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    05-14-2015
    Location
    Atlanta GA
    MS-Off Ver
    2007
    Posts
    16

    Re: Macro that grabs data from table

    When I run this now for some reason I get run time error 438 on the GetWebDocument = 'Error: line, with it saying the object doesnt support this property or method. What can I do to fix this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 04-10-2015, 04:34 PM
  2. Bloomberg Grabs using primo PDF
    By akshay8530 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-19-2014, 12:01 PM
  3. Replies: 1
    Last Post: 01-01-2014, 09:53 AM
  4. Almost done, Macro grabs excel cells copies to word
    By DadaaP in forum Excel Programming / VBA / Macros
    Replies: 20
    Last Post: 05-31-2013, 02:08 AM
  5. write formula that grabs data from last column
    By marlowisws in forum Excel General
    Replies: 0
    Last Post: 08-04-2006, 12:45 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1