How to Scrape Web Data Using Class Names with VBA

We can scrape web data using class names with VBA. “The class attribute specifies one or more classnames for an element. The class attribute is mostly used to point to a class in a style sheet. However, it can also be used by a JavaScript¬† via the HTML DOM ¬†to make changes to HTML elements with a specified class.”

Watch the video below to see how you can extract data from a web-page using the class attribute:


Watch the video on YouTube.

Here’s the complete VBA (macro) code:

Sub useClassnames()
Dim element As IHTMLElement
Dim elements As IHTMLElementCollection
Dim ie As InternetExplorer

Dim html As HTMLDocument

‘open Internet Explorer in memory, and go to website

Set ie = New InternetExplorer

ie.Visible = True

ie.navigate “”
‘Wait until IE has loaded the web page

Do While ie.readyState <> READYSTATE_COMPLETE

Application.StatusBar = “Loading Web page …”



Set html = ie.document

Set elements = html.getElementsByClassName(“result”)

Dim count As Long
Dim erow As Long
count = 0
For Each element In elements
If element.className = “result” Then
erow = Sheet1.Cells(Rows.count, 1).End(xlUp).Offset(1, 0).Row
Cells(erow, 1) = html.getElementsByTagName(“h2”)(count).innerText
Cells(erow, 2) = html.getElementsByClassName(“address”)(count).innerText
count = count + 1
End If
Next element

Columns(“B:B”).ColumnWidth = 36
‘MsgBox count
End Sub

Important: Before you run the above code you must activate the following as shown in the video and in the image below:

  1. Microsoft HTML Library
  2. Microsoft Internet Controls

Further reading:

Class Attribute in HTML







Published by

Dinesh Kumar Takyar

Welcome to! My aim is to help you learn MS Excel including VBA. I always felt that a good way to learn something was to find solutions to problems in that domain. That is why I share these Excel videos with you. Mostly these are questions asked during my corporate training sessions. I've been training individuals and companies in computers since 1991. I did my B. Sc. (Vordiplom), M. Sc. (Diplom) and Ph.D. (Dr. rer. nat.) from Hamburg, Germany. The best thing about solving some of my visitor's questions is that I also gain new insights. For more Excel VBA solutions you may like to visit my YouTube channel: For a structured Excel VBA training course online you can visit:

5 thoughts on “How to Scrape Web Data Using Class Names with VBA”

  1. Thanks for the code, however I get an error message run_time error ’13’ mismatch on line
    set “html = ie.document . a solution would be appreciated.

  2. i tried exact same code but i am getting user defined type notdefined for this variable declaration of IHTMLElement. Pleasehelp

  3. Hi, could you help me how to scrape a grilla with a lot of prices with each product wich have same link , all under div elements, without ID, as i can’t find the way. It seems can’t built a code for them

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.