Watch the video before you study the code to follow a link on a web page automatically with VBA:
A user wants to open a website automatically and then click on a specific link also automatically so that he can scrape data from that specific page and get it into an Excel worksheet. We have shown earlier how to scrape web page data automatically using VBA. Today we’ll learn how to follow a link on a web page automatically with VBA. The complete VBA code is given below:
Sub followWebsiteLink()
‘From Tools —-> References activate
‘1. Microsoft HTML Object Library
‘2. Microsoft Internet Controls
Dim ie As InternetExplorer
Dim html As HTMLDocument
Dim Link As Object
Dim ElementCol As Object
Application.ScreenUpdating = False
Set ie = New InternetExplorer
ie.Visible = True
ie.navigate “https://www.google.co.in”
Do While ie.readyState <> READYSTATE_COMPLETE
Application.StatusBar = “Loading website…”
DoEvents
Loop
Set html = ie.document
Set ElementCol = html.getElementsByTagName(“a”)
For Each Link In ElementCol
If Link.innerHTML = “Advertising” Then
Link.Click
End If
Next Link
Set ie = Nothing
Application.StatusBar = “”
Application.ScreenUpdating = True
End Sub
Download a sample Excel file: