Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Import Website Data to Excel

October 15, 2015 Marcus Small

I was recently asked to repair a webquery which had gone bad.  The company controlling the website had changed the layout of the content.  I decided to extract the data from the website into excel using VBA and a call on the HTML tables inside of the website itself.  Obviously this technique works best where there are dedicated tables on a website.  It does not discriminate as all data from the page is imported but with some smart manipulation you can inject some VBA to extract the relevant tables into a fresh worksheet.  

If you put the website address in A1 of sheet1 of your workbook the following will upload the tables from your website.  

https://afd.calpoly.edu/web/sample-tables

The above is the web address I will be downloading.  It is contained inside the file.

Option Explicit
Sub HTMLTable() 'Excel VBA to import website tables.
   Dim htm As Object
   Dim Tr As Object
   Dim Td As Object
   Dim Tab1 As Object
   Dim URL As String
   Dim Colstart As Long
   Dim HTML As Variant
   Dim i As Long
   Dim j As Long
   Dim n As Long

   Application.ScreenUpdating = False
   URL = VBA.Trim(Sheets(1).Cells(1, 1)) 'String
   Set HTML = CreateObject("htmlfile") 'Create HTMLFile Object
   With CreateObject("msxml2.xmlhttp") 'Get the WebPage Content
      .Open "GET", URL, False
      .send
      HTML.Body.Innerhtml = .responseText
   End With

   Colstart = 1
   j = 2
   i = Colstart
   n = 0

   'Loop Through website tables
   For Each Tab1 In HTML.getElementsByTagName("table")
      With HTML.getElementsByTagName("table")(n)
         For Each Tr In .Rows
            For Each Td In Tr.Cells
               Sheet1.Cells(j, i) = Td.innerText
               i = i + 1
            Next Td
            i = Colstart
            j = j + 1
          Next Tr
      End With
      n = n + 1
      i = Colstart
      j = j + 1
   Next Tab1
 Application.ScreenUpdating = True
End Sub

The following Excel file has the VBA procedure and an Example.  Enjoy!!!

Import Web Tables.xls

Tags Import, Web, Data, Excel, Spreadsheet, vba
← Add Picture to Excel Cell Convert Vertical Range to Horizontal with VBA →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.