Combat Womble
New Member
- Joined
- Nov 18, 2015
- Messages
- 30
Hello
To me this is complicated so i will break it down.
I want to write a macro that gets data and tables from different pages of a website. You need a login and password (which I have) to view the pages. (I can not get it with Get External Date or Power Query). If I have IE open already and log onto the website, I can get some data using the following code in VBA:
However I can not get anything from the tables (I ideally would like the whole table so I pick the data I want within Excel). I have searched the internet - including this website and I haven't been able to find anything that works when i try it. The closest I've got is the following code.
The code came with the message "I wrote this code earlier this week. It will search for the first table and copy all the data from the HTML table minus the headers to the active sheet starting at A1. Put your HTML address under the ie.navigate line between the first quotes"
The code appears to do exactly what the message states.
Please can someone 1) explain how I can change this code to either pick a table or get all the tables,
or 2) suggest a code which will enable me to put the tables from the web page into excel.
Thank you in advance
Combat Womble
P.S. the rest of the code I need I will try and write myself. Its just the getting the table into excel I'm struggling with.
To me this is complicated so i will break it down.
I want to write a macro that gets data and tables from different pages of a website. You need a login and password (which I have) to view the pages. (I can not get it with Get External Date or Power Query). If I have IE open already and log onto the website, I can get some data using the following code in VBA:
Code:
Sub GetData()
Dim ie As New InternetExplorer
'IE.Visible = True
ie.navigate "https://www.anywebsite.com"
Do
DoEvents
Loop Until ie.readyState = READYSTATE_COMPLETE
Dim doc As HTMLDocument
Set doc = ie.document
Dim Event1 As String
Event1 = LettersOnly(doc.getElementsByTagName("a")(76).innerText)
Dim Event2 As String
Event2 = LettersOnly(doc.getElementsByTagName("a")(77).innerText)
Dim Event3 As String
Event3 = LettersOnly(doc.getElementsByTagName("a")(78).innerText)
Dim Event4 As String
Event4 = LettersOnly(doc.getElementsByTagName("a")(79).innerText)
Dim Event5 As String
Event5 = LettersOnly(doc.getElementsByTagName("a")(80).innerText)
Dim Event6 As String
Event6 = LettersOnly(doc.getElementsByTagName("a")(81).innerText)
Dim Event7 As String
Event7 = LettersOnly(doc.getElementsByTagName("a")(82).innerText)
Dim Event8 As String
Event8 = LettersOnly(doc.getElementsByTagName("a")(83).innerText)
Dim Event9 As String
Event9 = LettersOnly(doc.getElementsByTagName("a")(84).innerText)
Dim Event10 As String
Event10 = LettersOnly(doc.getElementsByTagName("a")(85).innerText)
MsgBox Event1 & Event2 & Event3 & Event4 & Event5 & Event6 & Event7 & Event8 & Event9 & Event10
ie.Quit
Application.Wait (Now + TimeValue("0:00:02"))
Set ie = Nothing
End Sub
However I can not get anything from the tables (I ideally would like the whole table so I pick the data I want within Excel). I have searched the internet - including this website and I haven't been able to find anything that works when i try it. The closest I've got is the following code.
The code came with the message "I wrote this code earlier this week. It will search for the first table and copy all the data from the HTML table minus the headers to the active sheet starting at A1. Put your HTML address under the ie.navigate line between the first quotes"
Code:
Dim ie As Object, 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, td 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
Set ie = CreateObject("InternetExplorer.Application")
ie.Visible = False
y = 1 'Column A in Excel
z = 1 'Row 1 in Excel
ie.navigate "https://www.anywebsite.com", , , , "Content-Type: application/x-www-form-urlencoded" & vbCrLf
Do While ie.Busy: DoEvents: Loop
Do While ie.readyState <> 4: DoEvents: Loop
Set doc = ie.document
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
ws.Cells(z, y).Value = td.innerText
y = y + 1
Next td
DoEvents
z = z + 1
Next tr
Exit For
Next bb
Exit For
Next tb
ie.Quit 'i have added this
Application.Wait (Now + TimeValue("0:00:02")) 'i have added this
Set ie = Nothing 'i have added this
End Sub
The code appears to do exactly what the message states.
Please can someone 1) explain how I can change this code to either pick a table or get all the tables,
or 2) suggest a code which will enable me to put the tables from the web page into excel.
Thank you in advance
Combat Womble
P.S. the rest of the code I need I will try and write myself. Its just the getting the table into excel I'm struggling with.
Last edited: