PDA

View Full Version : Import HTML Data into excel



abhay_547
01-21-2018, 09:36 PM
I got the below macro from a old thread. I have the lines on the html pages saved on my c drive which are supposed to be in a table format but they are not and there is no separator between each column data except some undefined spaces as shown in below image, so how can i import it as a proper table .i.e. into separate columns in the excel file, also i have multiple such html files so can i import multiple files into one worksheet at one go.



'Requires references to Microsoft Internet Controls and Microsoft HTML Object Library
Sub Extract_TD_text()

Dim URL As String
Dim IE As InternetExplorer
Dim HTMLdoc As HTMLDocument
Dim TDelements As IHTMLElementCollection
Dim TDelement As HTMLTableCell
Dim r As Long

'Saved from www vbaexpress com/forum/forumdisplay.php?f=17
URL = "file://C:\VBAExpress_Excel_Forum.html"

Set IE = New InternetExplorer

With IE
.navigate URL
.Visible = True

'Wait for page to load
While .Busy Or .readyState <> READYSTATE_COMPLETE: DoEvents: Wend

Set HTMLdoc = .document
End With

Set TDelements = HTMLdoc.getElementsByTagName("TD")

Sheet1.Cells.ClearContents

r = 0
For Each TDelement In TDelements
'Look for required TD elements - this check is specific to VBA Express forum - modify as required
If TDelement.className = "alt2" And TDelement.Align = "center" Then
Sheet1.Range("A1").Offset(r, 0).Value = TDelement.innerText
r = r + 1
End If
Next

End Sub





attached is the html image.

paulked
01-21-2018, 10:25 PM
Cross posted here https://www.mrexcel.com/forum/excel-questions/1039906-extract-data-tables-html-files-into-excel-worksheet.html (https://www.mrexcel.com/forum/excel-questions/1039906-extract-data-tables-html-files-into-excel-worksheet.html)

paulked
01-21-2018, 10:29 PM
But the code is different!!