Manually copying and pasting large tables from websites into Microsoft Excel is often tedious. Formatting gets distorted, numbers end up in the wrong cells, and many websites actively disable right-clicking or text selection.
By combining Excel VBA with Selenium WebDriver, you can automatically extract any HTML table from the web directly into your active worksheet with original rows, headers, and columns fully intact.
Prerequisites
Before writing the automation code, make sure you have:
SeleniumBasic installed on your system.
The matching ChromeDriver executable placed in your
SeleniumBasicfolder (C:\Program Files\SeleniumBasic).Google Chrome installed.
How to Install Selenium Basic & ChromeDriver (English)
Selenium Basic और ChromeDriver कैसे Install करें? | हिंदी में Step-by-Step Tutorial
Step 1: Launch Chrome with Remote Debugging
Connecting Selenium to an already open Chrome session bypasses login screens, bot-detection scripts, and clipboard restrictions.
Press
Win + Rto open the Windows Run dialog.Enter the following command and press Enter:
In this newly launched Chrome window, navigate to the webpage containing the table you want to scrape (e.g., world population or country data).
Right-click on the data and select Inspect Element to verify that it is wrapped inside a standard
tag.Step 2: Enable the Selenium Reference in Excel
Open Microsoft Excel and create a blank workbook.
Press
Alt + F11to launch the Visual Basic Editor (VBE).In the top menu, go to Tools > References.
Scroll down, check the box for Selenium Type Library, and click OK.
Step 3: Add the VBA Scraping Macro
Inside the Visual Basic Editor, click Insert > Module.
Paste the automation script into the module window:
VBAOption Explicit
Sub ExtractUniversalTableData()
Dim driver As Object
Dim candidateRows As Object, rowItem As Object
Dim headerCells As Object, dataCells As Object
Dim cellItem As Object
Dim ws As Worksheet
Dim r As Long, c As Long
Dim cellText As String
Dim colCount As Long
Dim rowsAdded As Long
Set ws = ActiveSheet
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
On Error GoTo ErrorHandler
Set driver = CreateObject("Selenium.ChromeDriver")
driver.SetCapability "debuggerAddress", "127.0.0.1:9222"
driver.Start "chrome", ""
If ws.Cells(ws.Rows.Count, 1).End(xlUp).Row > 1 Then
r = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row + 1
Else
r = 1
End If
Set headerCells = driver.FindElementsByXPath("(//tr[th])[1]/th")
If headerCells.Count = 0 Then
Set headerCells = driver.FindElementsByXPath("(//thead//tr)[1]/td")
End If
If headerCells.Count = 0 Then
Set headerCells = driver.FindElementsByXPath("(//table//tr)[1]/td")
End If
If r = 1 And headerCells.Count > 0 Then
c = 1
For Each cellItem In headerCells
cellText = Trim(cellItem.Text)
If cellText = "" Then cellText = "Column " & c
ws.Cells(r, c).Value = cellText
ws.Cells(r, c).Font.Bold = True
ws.Cells(r, c).Interior.Color = RGB(220, 230, 241)
c = c + 1
Next cellItem
r = r + 1
End If
Set candidateRows = driver.FindElementsByXPath("//tr[td[1][number(text()) = number(text())] and count(./td) >= 3]")
If candidateRows.Count = 0 Then
Set candidateRows = driver.FindElementsByXPath("//tbody/tr[td[2]]")
End If
If candidateRows.Count = 0 Then
Set candidateRows = driver.FindElementsByXPath("//table//tr[td]")
End If
If candidateRows.Count = 0 Then GoTo CleanExit
rowsAdded = 0
For Each rowItem In candidateRows
Set dataCells = rowItem.FindElementsByXPath("./td")
If dataCells.Count >= 1 Then
c = 1
For Each cellItem In dataCells
cellText = Trim(cellItem.Text)
If cellText = "" Then
On Error Resume Next
cellText = Trim(cellItem.FindElementByXPath(".//input | .//select").Attribute("value"))
On Error GoTo ErrorHandler
End If
If Left(cellText, 1) = "=" Or Left(cellText, 1) = "+" Or _
Left(cellText, 1) = "-" Or Left(cellText, 1) = "@" Then
ws.Cells(r, c).Value = "'" & cellText
Else
ws.Cells(r, c).Value = cellText
End If
c = c + 1
If c > 16384 Then Exit For
Next cellItem
r = r + 1
rowsAdded = rowsAdded + 1
End If
Next rowItem
ws.Columns.AutoFit
CleanExit:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Exit Sub
ErrorHandler:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "Scraping Error"
End SubStep 4: Run and Verify
Keep your target website open in the debugging Chrome window.
Return to Excel's VBA Editor and press
F5(or click the green Run icon).The script loops through all rows (
<tr>) and cells (<td>/<th>) inside the target table.Once finished, a dialogue stating "Extraction Complete" will pop up, and your sheet will be cleanly populated with the scraped dataset.
Leave a Comment
Your email address will not be published.
0 Comments
No comments yet. Be the first to comment.