Using Excel VBA to Scrape a Table from a Website – 4 Steps

This is a real-time data table from the investing.com website.

excel vba scrape table from website


STEP 1 – Open the Visual Basic Editor

  • Go to the Developer tab and select Visual Basic to open the VBA window. You can press Alt + F11 to open the VBA window.

Open Visual Basic Editor

  • Click Tools and select References.

  • In the References VBAProject window, check Microsoft HTML Object Library and Microsoft Internet Controls.
  • Click OK.


STEP 2 – Insert a Module

  • In VBA Projects, select the active sheet and rightclick it.
  • Select Module.

Insert Module Window


STEP 3 – Enter the VBA Code

  • Use the following code in the Module window.
Option Explicit
Sub get_table_web()
Dim ig As Object
Dim urlc As String
urlc = "https://uk.investing.com/rates-bonds/financial-futures"
Set ig = CreateObject("InternetExplorer.Application")
ig.Visible = True
ig.navigate urlc
Do While ig.busy: DoEvents: Loop
Do Until ig.readyState = 4: DoEvents: Loop
Dim tb As HTMLTable
Set tb = ig.document.getElementById("cr1")
Dim rowcounter As Integer
Dim columncounter As Integer
rowcounter = 4
columncounter = 2
Dim tro As HTMLTableRow
Dim tdc As HTMLTableCell
Dim thu
Dim mys As Worksheet
Set mys = ThisWorkbook.Sheets("Financial_Futures")
For Each tro In tb.getElementsByTagName("tr")
For Each thu In tro.getElementsByTagName("th")
mys.Cells(rowcounter, columncounter).Value = thu.innerText
columncounter = columncounter + 1
Next thu
For Each tdc In tro.getElementsByTagName("td")
    mys.Cells(rowcounter, columncounter).Value = tdc.innerText
columncounter = columncounter + 1
Next tdc
columncounter = 1
rowcounter = rowcounter + 1
Next tro
End Sub

Type VBA Code

VBA Code Explanation:

Sub get_table_web()
Dim ig As Object
Dim urlc As String
urlc = "https://uk.investing.com/rates-bonds/financial-futures"

the subprocedure get_table_web is created and variables are declared.

Set ig = CreateObject("InternetExplorer.Application")
ig.Visible = True
ig.navigate urlc
Do While ig.busy: DoEvents: Loop
Do Until ig.readyState = 4: DoEvents: Loop
Dim tb As HTMLTable
Set tb = ig.document.getElementById("cr1")

this part gets the table from the website.

Dim rowcounter As Integer
Dim columncounter As Integer
rowcounter = 4
columncounter = 2
Dim tro As HTMLTableRow
Dim tdc As HTMLTableCell
Dim thu

more variables are declared to select the cell reference to include the table in the worksheet.

Dim mys As Worksheet
Set mys = ThisWorkbook.Sheets("Financial_Futures")
For Each tro In tb.getElementsByTagName("tr")
For Each thu In tro.getElementsByTagName("th")
mys.Cells(rowcounter, columncounter).Value = thu.innerText
columncounter = columncounter + 1
Next thu

loops through the table headers.

For Each tdc In tro.getElementsByTagName("td")
    mys.Cells(rowcounter, columncounter).Value = tdc.innerText
columncounter = columncounter + 1
Next tdc
columncounter = 1
rowcounter = rowcounter + 1
Next tro

loops through the table cells and enters the data from the website table into the active worksheet.

Note:

  • urlc = “https://uk.investing.com/rates-bonds/financial-futures“: enter the URL of the website.
  • Set tb = ig.document.getElementById(“cr1”): Instead of “cr1” enter the Table ID by inspecting the website table.
  • Set mys = ThisWorkbook.Sheets(“Financial_Futures”):  “Financial_Futures” is the name of the active worksheet here.

STEP 4 – Save and Run the VBA Code

  • Click Save in the VBA window.
  • Click Run or press F5 to run the code.

Save and Run VBA Code


Final Output

  • Go back to the active worksheet.

Final Output excel vba scrape table from website


Download the Practice Workbook


Get FREE Advanced Excel Exercises with Solutions!
Mehedi Hasan Shimul
Mehedi Hasan Shimul

Md. Mehedi Hasan, with a BSc in Electrical & Electronic Engineering from Bangladesh University of Engineering and Technology, holds a crucial position as an Excel & VBA Content Developer at ExcelDemy. Driven by a deep passion for research and innovation, he actively immerses himself in Excel. In his role, Mehedi not only skillfully addresses complex challenges but also exhibits enthusiasm and expertise in gracefully navigating tough situations, emphasizing his steadfast commitment to consistently deliver exceptional and quality content.... Read Full Bio

1 Comment
  1. Greetings and thanks for the article, which I have used with satisfactory results. I wonder if with the intention of making this Code more functional, you could give me the solution to be able to access the combo box of the temporality (Daily, weekly, Monthly) and the Datepicker to be able to send the personalized dates from Excel.
    Thank-you

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo