0

I'm trying to get the content of a table on an internal web page down into an excel sheet, but I can't grab any of the HTML elements. I have tried grabbing all the elements by ID, Classname and tagname but to no avail. Can someone give me a clue on what I'm doing wrong?

Sub Shuffle()

Application.DisplayAlerts = False
'On Error GoTo ErrHandler
Dim SheetName, FavNumber As String


'LTI
SheetName = "LTI"
FavNumber = "2282804"
'Call IE_Sledgehammer


   Dim ie As InternetExplorerMedium, I As Long, strText As String
   Dim doc As Object, hTable As Object, hBody As Object, hTR As Object, hTD As Object
   Dim tb As Object, bb As Object, tr As Object
   Dim y As Long, z As Long, wb As Excel.Workbook, ws As Excel.Worksheet



   Set wb = Excel.ActiveWorkbook
   'Set ws = wb.ActiveSheet
   
   
'LastRow = Sheets(SheetName).Cells(Sheets(SheetName).Rows.Count, "A").End(xlUp).Row
'If LastRow > 1 Then
'    Sheets(SheetName).Range("A2:AA" & LastRow + 1).ClearContents
'End If
'Application.Wait (Now + TimeValue("0:00:02"))

Set ie = Nothing
Application.Wait (Now + TimeValue("0:00:02"))
Set ie = New InternetExplorerMedium
ie.Visible = True


 ie.navigate "https://synergi.de-prod.dk/synergi/favourite/" & FavNumber
 On Error Resume Next
 Do While ie.Busy: DoEvents: Loop
 Do While ie.readyState <> 4:
 

 Set doc = ie.document
 
 Dim td As Object
 Set td = doc.getElementsByTagName("td")
 Set Post6a = doc.getElementsByTagName("th")(0).outerText: MsgBox Post6a
 Set Post6b = doc.getElementsByTagName("th")(0).innerText: MsgBox Post6b
 Set Post6c = doc.getElementsByTagName("td"): MsgBox Post6c.Length
 
 Dim th As Object
 Set th = doc.getElementsByTagName("th")
 Set Post8a = doc.getElementsByTagName("td")(0).outerText: MsgBox Post8a
 Set Post8b = doc.getElementsByTagName("td")(0).innerText: MsgBox Post8b
 Set Post8c = doc.getElementsByTagName("th"): MsgBox Post8c.Length
 
 Dim span As Object
 Set span = doc.getElementsByTagName("span")
 Set Post7a = doc.getElementsByTagName("span")(0).outerText: MsgBox Post7a
 Set Post7b = doc.getElementsByTagName("span")(0).innerText: MsgBox Post7b
 Set Post7c = doc.getElementsByTagName("span"): MsgBox Post7c.Length

 Dim div As Object
 Set span = doc.getElementsByTagName("div")
 Set Post9a = doc.getElementsByTagName("div")(0).outerText: MsgBox Post9a
 Set Post9b = doc.getElementsByTagName("div")(0).innerText: MsgBox Post9b
 Set Post9c = doc.getElementsByTagName("div"): MsgBox Post9c.Length
 
     
  y = 1   'Column A in Excel
  z = 2   'Row 1 in Excel
  
Set hTable = doc.getElementsByTagName("table")
 
 For Each tb In hTable

    Set hBody = tb.getElementsByTagName("tbody")
    For Each bb In hBody

        Set hTR = bb.getElementsByTagName("tr")
        For Each tr In hTR


             Set hTD = tr.getElementsByTagName("td")
             y = 1 ' Resets back to column A
             For Each td In hTD
               wb.Sheets(SheetName).Cells(z, y).Value = td.innerText
               y = y + 1
               'MsgBox td.innerText
             Next td
             DoEvents
             z = z + 1
        Next tr
        Exit For
    Next bb
Exit For
  Next tb

Application.Wait (Now + TimeValue("0:00:02"))
'IE.Quit
ie.Quit
End Sub

The HTML code for the web page is the following: enter image description here

1 Answer 1

0

If you need to extract the table content from the website to Excel, you can try these two methods:

  1. Using IE: The data is inside an iframe which needs to be negotiated

  2. Using XMLHTTP request - much faster and without browser opening. It uses the first part of the iframe document URL which is what the iframe is navigating to.

For more details, you can refer to the following case, which contains a simple example: Extract table from webpage using VBA

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.