Here is a breakdown of what I am trying to accomplish:
1)Take a value from a list in the spreadsheet
2)Search a URL with that value
3)Take an ElementId
from the HTML and add this to the spreadsheet (this would not need to be added to the spreadsheet, but I need it for the next search and was not sure how to do it otherwise)
4)Use the ElementId to search another URL
5)Pull the value of a ClassName
from the HTML
My problem comes when a value in the list does not have an IDValue
from my search in the HTML's ElementId
. I need to skip those values and GoTo NextName
, but have not found a successful way of doing so.
I have tried adding a timer to the Do Loop, but still have not been able to advance to the next i
in the list.
Here is my code:
Option Explicit
Public Sub CheckNames()
Dim ws As Worksheet, i As Long, IE As InternetExplorerMedium
Dim doc As HTMLDocument, docElement As Object
Dim docClass As Object
Set ws = ActiveSheet
Set IE = New InternetExplorerMedium
1)
For i = 2 To ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
IE.Visible = False
2)
IE.navigate "Beginning of URL" & ws.Cells(i, 1).Value & "End of URL"
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
IE.Refresh
Set doc = IE.document
3)
Do
Set docElement = Nothing
On Error Resume Next
Set docElement = doc.getElementById("IDValue")
On Error GoTo NextName
DoEvents
Loop Until Not docElement Is Nothing
ws.Cells(i, 3).Value = Right(Trim(docElement.innerText), 5)
4)
IE.navigate "Beginning of another URL" & ws.Cells(i, 3).Value & "End of URL"
5)
Do
Set docClass = doc.getElementsByClassName("CLass Name in HTML")(3)
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
ws.Cells(i, 4).Value = Trim(docClass.innerText)
Set doc = Nothing
Set docElement = Nothing
NextName:
Next i
IE.Quit
End Sub
So in short, I am looking for a way to skip to the next i
in the list when doc.getElementById("IDValue")
does not have a value.
Thanks in advance!