Excel VBA: Web Scraping with Chrome (With Easy Steps)

To demonstrate how to use Excel VBA to scrape data from a website using the Chrome browser, we’ll use this article from ExcelDemy as the dataset, and scrape the following table from the website.

Dataset fot Using Excel VBA for Web Scraping with Chrome


Step 1 – Install Selenium

In order to scrape data from websites in Excel, the Selenium library must be installed.

  • Click on the marked portion in the following image to download SeleniumBasic.

Install Selenium for Web Scraping with Chrome using VBA in Excel

  • Double-click on the downloaded file to install SeleniumBasic.

  • A Setup window will appear.
  • Select Next.

Setup Window for Selenium Basic for Scraping Web with Chrome in Excel VBA

  • Select I accept the agreement.
  • Select Next.

Another window will appear.

  • Click on the marked drop-down option.
  • Select Compact installation.

  • Click Next.

  • You will see the path of the Install folder. Make a note of it, we’ll need it later.
  • Select Install.

Selenium will be installed.

  • Select Finish.

Read More: Excel VBA to Scrape Table from Website


Step 2 – Download Chrome Driver

To scrape data from Chrome in Excel, we need the Chrome Driver.

Download Chrome Driver gor Web Scraping With Chrome in Excel VBA

Before downloading, you will need to know the Chrome version you are using.

  • To check your version, in Chrome click on the Three Dot button.
  • Select Help.
  • Select About Google Chrome.

  • Make a note of the Chrome version.

  • Download the ChromeDriver for your Chrome version by clicking on the appropriate link.

This will lead you to the download link.

  • Download the driver for your operating system.

  • In your File Explorer, select the downloaded zip file.
  • Right-click on the selected file.
  • Select Extract to chromedriver_win32(1)\.

Extracting Chrome Driver for Web Screaping with Chrome in Excel VBA

  • Open the folder that will be created.

  • Select the file and copy it by pressing Ctrl + C on your keyboard.

  • Go to the your install folder.
C:\Users\HP\AppData\Local\SeleniumBasic
  • Press Ctrl + V to paste the ChromeDriver in this folder.


Step 3 – Refer to Selenium Library in VBA

Now, we refer to the Selenium Library in VBA.

  • Go to the Developer tab.
  • Select Visual Basic.

Refer to Selenium Library in VBA for Web Scraping with Chrome

  • The Visual Basic editor window will open.
  • Select Tools.
  • Select References.

  • The References dialog box will appear.
  • Select Selenium Type Library.
  • Select OK.


Step 4 – Inspect Data Structure from Web

Before writing the code we must inspect the elements of the website we’ll be scraping and observe how the data are structured.

  • Right-click on the table from where you want to scrape data.
  • Select Inspect.

Inspect Data Structure from Web for Scraping Web with Chrome

You will be able to see how the data is structured and see the class, name, and tag of the data. These pieces of information will be needed to write the VBA code.


Step 5 – Write VBA Code

Now we have everything we need to write the VBA code for scraping web with Chrome in Excel.

  • Select the Insert tab.
  • Select Module.

Write VBA Code for Scraping Web with Chrome in Excel VBA

A Module will open.

  • Enter the following code in the Module:
Sub scraping_web()
Dim chrm As Selenium.ChromeDriver
Dim row_no, col_no As Integer
row_no = 2
Application.ScreenUpdating = False
Set chrm = New Selenium.ChromeDriver
    chrm.Start
    chrm.Get "https://www.exceldemy.com/excel-vba-translate-formula-language/"
For Each tr In chrm.FindElementByClass("wpsm-table"). _
FindElementByTag("tbody").FindElementsByTag("tr")
    col_no = 2
    For Each td In tr.FindElementsByTag("td")
        Sheet1.Cells(row_no, col_no).Value = td.Text
        col_no = col_no + 1
    Next td
    row_no = row_no + 1
Next tr
Application.Wait Now + TimeValue("00:00:20")
End Sub

How Does the Code Work?

  • We created a Sub Procedure named scraping_web.
  • We declared a variable named chrm as Selenium.ChromeDriver.
  • We declared two more variables as Integer.
  • We used the Application.ScreenUpdating property to speed up the macro.
  • We set chrm as New Selenium.ChromeDriver.
  • We used chrm.Start to start the Chrome browser.
  • We used chrm.Get to open the web page we want, by providing the URL of the page.
  • We used a For Next Loop to get the data from the table.
  • We used the FindElementByClass and FindElementsByTag methods to get the table elements.
  • We used the Application.Wait method to pause running the macro for a specified amount of time.
  • We ended the Sub Procedure.
  • Save the code and go back to your worksheet.

Saving VBA Code for Web Scraping with Chrome in Excel VBA


Step 6 – Insert Button and Assign Macro

To conveniently run the code, we’ll insert a Button and assign a Macro to it.

  • Go to the Developer tab.
  • Select Insert.
  • Select Button from Form Controls.

Insert Button and Assign Macro fot Web Scraping with Chrome in Excel VBA

  • Click and drag your mouse cursor to where you want the button.

The Assign Macro dialog box will appear.

  • Select scraping_web as the Macro name.
  • Click OK to assign the Macro to the button.

Assigning Macro to a Button for Web Scraping with Chrome in Excel VBA

  • Right-click on the button.
  • Select Edit Text.

  • Edit the text label for the button as desired. Here, we enter Scrape Data.

Our button looks like this:


Step 7 – Run VBA Code

  • Click on the Button.

Run VBA Code for Web Scraping with Chrome in Excel VBA

The data has been scraped from the table into Sheet 1.

  • Format the table according to your preference.


Final Output

Final Output of Web Scraping with Chrome in Excel VBA


Alternative Way to Scrape Table Data from Web in Excel

There is an an alternative way of scraping the web using Excel VBA without the Chrome driver.

Steps:

  • Open the Visual Basic Editor window by following Step 3.
  • Open a Module by following Step 5.
  • Enter the following code in the Module:
Option Explicit
Public Sub calling_sub()
    Call clear_sheet
    Call Scraping_Table
End Sub
Private Sub clear_sheet()
    Dim scrape_table As QueryTable
    For Each scrape_table In Sheet2.QueryTables
        scrape_table.Delete
    Next scrape_table
    Sheet2.Cells.Clear
End Sub
Public Sub Scraping_Table()
    Dim url_str As String
    url_str = "https://www.exceldemy.com/excel-vba-translate-formula-language/"
    Dim scrape_table As QueryTable
    Set scrape_table = Sheet2.QueryTables.Add("URL;" & url_str, Sheet2.Range("A1"))
    With scrape_table
        .WebSelectionType = xlSpecifiedTables
        .WebTables = "1"
        .WebFormatting = xlWebFormattingAll
        .Refresh
    End With
End Sub

Alternative Way to Scrape Table Data from Web

How Does the Code Work?

Public Sub calling_sub()
    Call clear_sheet
    Call Scraping_Table
End Sub
  • We created a Public Sub Procedure named calling_sub.
  • We used the Call Statement to call 2 Sub Procedures.
  • We ended the Public Sub Procedure.
Private Sub clear_sheet()
    Dim scrape_table As QueryTable
    For Each scrape_table In Sheet2.QueryTables
        scrape_table.Delete
    Next scrape_table
    Sheet2.Cells.Clear
End Sub
  • We created a Private Sub Procedure named clear_sheet.
  • We declared a variable named scrape_table as QueryTable.
  • We used a For Next Loop to go through the table.
  • We used the scrape_table.Delete method to delete objects.
  • We used the Sheet2.Cells.Clear method to clear objects entirely from Sheet2.
  • We ended the Private Sub Procedure.
Public Sub Scraping_Table()
    Dim url_str As String
    url_str = "https://www.exceldemy.com/excel-vba-translate-formula-language/"    
    Dim scrape_table As QueryTable
    Set scrape_table = Sheet2.QueryTables.Add("URL;" & url_str, Sheet2.Range("A1"))
    With scrape_table
        .WebSelectionType = xlSpecifiedTables
        .WebTables = "1"
        .WebFormatting = xlWebFormattingAll
        .Refresh
    End With
End Sub
  • We created a Public Sub Procedure named Scraping_Table.
  • We declared a variable named url_str as String.
  • We set the url_str as the URL of the website from where we want to scrape data.
  • We used the Set statement to determine the scrape_table.
  • We used the QueryTables.Add method to create a new query table.
  • We used the With Statement to execute a series of statements on a single object.
  • We used the WebSelectionType property to specify from where to scrape data.
  • We used the WebTables property to specify which table to select.
  • We used the WebFormatting property to specify the formatting we want. Here, we used xlWebFormattingAll to keep the formatting the same as the website.
  • We used the Refresh method to update the external data range.
  • We ended the With Statement.
  • We ended the Public Sub Procedure.
  • Save the code and go back to your worksheet.

  • Go to the Developer tab.
  • Select Macros.

The Macro dialog box will open.

  • Select calling_sub as the Macro name.
  • Click Run.

The specified table is scraped into the selected worksheet.

Read More: How to Do Web Scraping Without Browser with Excel VBA


Things to Remember

  • Whenever working with VBA, you must save your Excel file as Excel Macro-Enabled Workbook.
  • You must be connected to the Internet to run these codes.
  • Your ChromeDriver and Chrome application must be the same version.

Download Practice Workbook


Related Articles

Get FREE Advanced Excel Exercises with Solutions!
Mashhura Jahan
Mashhura Jahan

Mashhura Jahan, BSc in Industrial and Production Engineering from Bangladesh University of Engineering and Technology, invested 1.5 years at ExcelDemy. In roles like Excel & VBA Content Developer, Excel Charts, and Dashboard course trainer, she wrote 90+ articles. She was previously part of the forum support team and a junior software analyst on the Excel Add-in project. With interests spanning Excel, VBA, Power Query, Python, Data Science, and Software Development, Mashhura brings a diverse skill set to her... Read Full Bio

2 Comments
  1. Thank you for your post on “Excel VBA: Web Scraping with Chrome”. It was very helpful. I modified it to get the text from “a” tags and it worked. However, how do you extract the URL from the “a” tag? Thanks.

    • Hi Dennis Ryan,
      Thank you very much for reading our articles. You mentioned that you have modified the code to get texts. But you want to get URLs by Web Scraping with Chrome. You can try the following VBA code for that.

      VBA Macro:

      Sub scraping_web_urls()
      Dim chrm As New ChromeDriver
      Dim objLinks As Object
      Dim objLink As Object
      Dim iRow As Long
      chrm.Get “https://www.exceldemy.com/excel-vba-translate-formula-language/”
      chrm.Wait 4000
      Set objLinks = chrm.FindElementsByTag(“a”)
      iRow = 3
      For Each objLink In objLinks
      ActiveSheet.Cells(iRow, 2).Value = objLink.Attribute(“href”)
      iRow = iRow + 1
      Next objLink
      chrm.Quit
      End Sub

      VBA Macro for web scraping with Google Chrome to get URLs

      Code Breakdown

      Dim chrm As New ChromeDriver, Dim objLinks As Object, Dim objLink As Object, Dim iRow As Long
      1st we declared different variables with type.

      chrm.Get “https://www.exceldemy.com/excel-vba-translate-formula-language/”
      Navigate to a specific web link.

      chrm.Wait 4000
      Waits for 4000 milliseconds or 40 seconds.

      Set objLinks = chrm.FindElementsByTag(“a”)
      Sets the value of the objLinks variable.

      iRow = 3
      Set the value of iRow to 3.

      For Each objLink In objLinks
      ActiveSheet.Cells(iRow, 2).Value = objLink.Attribute(“href”)

      Find each objLink from objLinks. And paste them in the second column of row 3. Here, href is used for getting hyperlinks.

      iRow = iRow + 1
      Increase the value of iRow to move to the next row.

      chrm.Quit
      This tells the ChromeDriver to quit Google Chrome.

      Look at the output. This will extract all the URLs that exist in the given link.

      Output

      More URLs are present in this sheet.

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo