Error Handling on VBA Excel (Web Scraper)

Viewed 24

This is the program I created. The goal of this program is to visit each link on a specific cell range and get the "a href" of each listed links.

I used a list of links but there's a certain link that ends with .pdf and from there I get a type mismatch. Is there a way that I could make my program continue and just skip the error that it got from a specific link?

This is the link that causes the error https://ir-web-assets-v.s3.amazonaws.com/uploads/nuggets/5d40644eafe17554cf969aab/Islands_Locals_Program_Guest_FAQ.pdf

Sub extensiveScrape()
Dim extractedLinks As Range 'Links taken from RUN
Dim urls As String 'Links taken from Extensive Search
Dim appIE As Object
Dim LastRow As Long 'Number of rows
Dim rCell As Range
Dim rRng As Range
Dim html2 As HTMLDocument
Dim itemEle As Object
Dim linkurl As Object
Dim y As Integer

Application.ScreenUpdating = False
Set appIE = CreateObject("InternetExplorer.Application")

Set sht = ThisWorkbook.Worksheets("results")
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

Set rRng = sht.Range("A1:A" & LastRow)
For Each rCell In rRng.Cells
    With appIE
        .navigate rCell.Value
        .Visible = True
    End With
    
    Do While appIE.readyState <> 4: Wait 5: Loop
    Application.StatusBar = "Scraping Extensively..."
    DoEvents
    
    Set html2 = appIE.document
    Set itemEle = html2.getElementsByTagName("a")
    y = 1
    For Each linkurl In itemEle
    Sheets("results").Range("B" & y).Value = linkurl
    y = y + 1
    Next
    'rCell.Offset(0, 1).Value = itemEle
Next rCell

appIE.Quit
Set appIE = Nothing
Application.StatusBar = ""
Application.ScreenUpdating = True
End Sub

Private Sub Wait(ByVal nSec As Long)
    nSec = nSec + Timer
    While nSec > Timer
        DoEvents
    Wend
End Sub
0 Answers
Related