Saturday, March 12, 2016

Importing HTML file in Excel using VBA

This proc opens HTML files in excel, copy it into the sheet and then close the html without saving.
This ways you can import data from various html files into excel.
This can be modified to import values from other format like XML also.
This is a although very mundane way but VBA 6 is very old, VBA.NET might have something better.

For learning more VBA skills please check out our courses at

Sub Import()
Dim oExcel As Excel.Application
Dim ws, ws2, ws3 As Worksheet
Dim wb As Variant
Dim wkb As Variant

Set activewkb = ThisWorkbook
Path = ActiveWorkbook.Path

   Dim StrFile As String
    Path = ActiveWorkbook.Path
   ' MsgBox Path
    StrFile = Dir(Path & "\HTML\")
    Do While Len(StrFile) > 0
        MsgBox StrFile
        StrFile = Dir
   ' MsgBox StrFile
    Set wb = Workbooks.Open(Path & "\HTML\" & StrFile)
On Error Resume Next
'MsgBox ActiveWorkbook.Path

For Each sh In wb.Sheets
      sh.Copy after:=activewkb.Sheets(activewkb.Sheets.Count)
   Next sh

'MsgBox "The name of the active workbook is " & ActiveWorkbook.Name
wb.Close savechanges:=False

End Sub

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.