Hello,
I have two bits of code which do a simple scrape of internet websites. In both of them, if no match is found then there is an error message:
Run-time error '91':
Object variable or With block variable not set
And when I press 'Debug' these rows are highlighted, as shown below:
Sub Demo()
Dim URL As String
Dim ieDoc As Object
Dim cel As Range
For Each cel In Range("B2", Range("B" & Rows.Count).End(xlUp))
URL = cel.Value
With CreateObject("InternetExplorer.Application")
.Visible = False
.Navigate URL
Do Until .ReadyState = 4: DoEvents: Loop
Set ieDoc = .Document
cel.Offset(, 1).Value = ieDoc.getElementsByClassName("_gll")(0).getElementsByTagName("a")(0).href
.Quit
End With
Next cel
End Sub
Sub DemoMutual2()
Dim URL As String
Dim ieDoc As Object, dObj As Object
Dim cel As Range
URL = Range("G2").Value
With CreateObject("InternetExplorer.Application")
.Visible = False
.Navigate URL
Do Until .ReadyState = 4: DoEvents: Loop
Set ieDoc = .Document
Set dObj = ieDoc.getElementsByClassName("_50f3")
[G4].Value = Split(ieDoc.getElementsByClassName("_50f3")(0).innerText, " ")(0)
For i = 0 To dObj.Length - 1
If InStr(1, dObj(i).getElementsByTagName("a")(0).innerText, "since") Then
[G5].Value = Trim(Split(dObj(i).getElementsByTagName("a")(0).innerText, "since")(1))
End If
Next
.Quit
End With
Set ieDoc = Nothing
Set dObj = Nothing
End Sub
Obviously the reason for the errors is because this part of the webpage is not there to be scraped, so I am wondering if my code could be amended slightly so that instead of there being an error, there is something displayed in Excel - such as 'No Link' for example.
The relevant cells for these error messages would be C2 for the first code and G4 for the second.
Any help much appreciated,
Cheers
Bookmarks