DesignerKB2
12-15-2014, 06:03 PM
Hi,
I've written some VBA code to scrape information from a website using .document.getElementsByTagName and .document.getElementsByClassName however the webpage is dynamic and as a result the object I’m looking for doesn’t always appear in the source code. When this happens I get an “Avoiding run time error '424': object required” appear. Instead of getting this error I just want the Macro to realise that the information doesn’t exist and then move on to the next bit of code/loop rather than stop the macro. Any ideas?
Thanks,
Kieran
The code is here
Sub Select_Cells()
Dim cellrow As Integer
Dim cellcol As Integer
Dim landsize As String
cellrow = 25
cellcol = 3
Cells(cellrow, cellcol).Select
Dim IE As Object
Dim webpage As String
Dim t As Integer, r As Integer, c As Integer
Dim elemCollection As Object
Dim guesstimate As Object
For cellrow = 25 To 25
webpage = "inser property profile for 36 Goodwood Street, RICHMOND VIC 3121 for the onthehouse website in australia"
Set IE = CreateObject("internetexplorer.application")
With IE
.navigate webpage
While IE.ReadyState <> 4
DoEvents
Wend
Set elemCollection = IE.document.getElementsByTagName("TABLE")
ThisWorkbook.Worksheets(2).Cells(cellrow, 6) = elemCollection(0).Rows(1).Cells(1).innerText
ThisWorkbook.Worksheets(2).Cells(cellrow, 7) = elemCollection(2).Rows(1).Cells(0).innerText
ThisWorkbook.Worksheets(2).Cells(cellrow, 8) = elemCollection(2).Rows(1).Cells(3).innerText
Set guesstimate = IE.document.getElementsByClassName("guesstimate-value-estimate")
For t = 0 To (guesstimate.Length - 1)
ThisWorkbook.Worksheets(2).Cells(cellrow, 9) = guesstimate(t).innerText
Next t
End With
Next cellrow
Set IE = Nothing
MsgBox ("Complete")
End Sub
I've written some VBA code to scrape information from a website using .document.getElementsByTagName and .document.getElementsByClassName however the webpage is dynamic and as a result the object I’m looking for doesn’t always appear in the source code. When this happens I get an “Avoiding run time error '424': object required” appear. Instead of getting this error I just want the Macro to realise that the information doesn’t exist and then move on to the next bit of code/loop rather than stop the macro. Any ideas?
Thanks,
Kieran
The code is here
Sub Select_Cells()
Dim cellrow As Integer
Dim cellcol As Integer
Dim landsize As String
cellrow = 25
cellcol = 3
Cells(cellrow, cellcol).Select
Dim IE As Object
Dim webpage As String
Dim t As Integer, r As Integer, c As Integer
Dim elemCollection As Object
Dim guesstimate As Object
For cellrow = 25 To 25
webpage = "inser property profile for 36 Goodwood Street, RICHMOND VIC 3121 for the onthehouse website in australia"
Set IE = CreateObject("internetexplorer.application")
With IE
.navigate webpage
While IE.ReadyState <> 4
DoEvents
Wend
Set elemCollection = IE.document.getElementsByTagName("TABLE")
ThisWorkbook.Worksheets(2).Cells(cellrow, 6) = elemCollection(0).Rows(1).Cells(1).innerText
ThisWorkbook.Worksheets(2).Cells(cellrow, 7) = elemCollection(2).Rows(1).Cells(0).innerText
ThisWorkbook.Worksheets(2).Cells(cellrow, 8) = elemCollection(2).Rows(1).Cells(3).innerText
Set guesstimate = IE.document.getElementsByClassName("guesstimate-value-estimate")
For t = 0 To (guesstimate.Length - 1)
ThisWorkbook.Worksheets(2).Cells(cellrow, 9) = guesstimate(t).innerText
Next t
End With
Next cellrow
Set IE = Nothing
MsgBox ("Complete")
End Sub