Option Explicit
Sub ImportHTMLTextProducts()
'Open all .TXT in specific folder and import data
'JBeaucaire (8/6/2009) (2007 compatible)
Dim fName As String, fPath As String, buf As String
Dim Count As Long, NR As Long, cFind As Long
Dim wNew As Workbook, v As Boolean
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
'Setup this report
Set wNew = ThisWorkbook
v = Evaluate("ISREF(Products!A1)")
If Not v Then
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Products"
Range("A1") = "Product Name"
Range("B1") = "Price"
Range("C1") = "Date"
Range("D1") = "Time"
With Range("A1:D1")
.Font.Bold = True
.Borders(xlEdgeBottom).LineStyle = xlContinuous
.Borders(xlEdgeBottom).Weight = xlMedium
.Interior.ColorIndex = 35
End With
Columns("B:B").NumberFormat = "$#,##0.00"
Columns("C:C").NumberFormat = "m/d/yyyy"
Range("A2").Select
ActiveWindow.FreezePanes = True
wNew.Activate
Else
Sheets("Products").Range("A2:D" & Rows.Count).ClearContents
End If
'Setup File Listing
fPath = "C:\webpages\"
If Right(fPath, 1) <> "\" Then fPath = fPath & "\"
fName = Dir(fPath & "*.txt")
NR = 2
'Import Files
Do While Len(fName) > 0
Workbooks.OpenText Filename:=fPath & fName, Origin:=437, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(1, 2), TrailingMinusNumbers:=True
cFind = Range("A:A").Find(What:="a href", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Row + 1
Count = 0
Do
'Get Products
Range("A:A").Find(What:="a href", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
If ActiveCell.Row < cFind And Count > 0 Then Exit Do
buf = ActiveCell.Text
buf = Left(buf, InStr(buf, "</a>") - 1)
buf = Right(buf, Len(buf) - InStrRev(buf, ">"))
wNew.Sheets("Products").Range("A" & NR) = buf
'Get Time
Range("A:A").Find(What:="PDT", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
buf = Left(ActiveCell.Text, InStr(ActiveCell.Text, "PDT") + 2)
wNew.Sheets("Products").Range("D" & NR) = Right(buf, 9)
'Get Price
Range("A:A").Find(What:="$", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
buf = ActiveCell.Text
buf = Left(buf, InStr(buf, "</strong>") - 1)
buf = Right(buf, Len(buf) - InStrRev(buf, "$") + 1)
wNew.Sheets("Products").Range("B" & NR) = buf
'Get Date
Range("A:A").Find(What:="</div>", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
buf = ActiveCell.Text
buf = Left(buf, InStr(buf, "</div>") - 1)
wNew.Sheets("Products").Range("C" & NR) = Right(buf, 10)
NR = NR + 1
Count = Count + 1
Loop
'Get next filename ready
fName = Dir
ActiveWorkbook.Close False
Loop
'Cleanup
Columns("A:D").AutoFit
Application.DisplayAlerts = True
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Bookmarks