Sub test()
Dim IE As New InternetExplorer
Dim city$, state$
Dim i As Integer
Dim dd As Variant
Dim doc As HTMLDocument 'variable for document or data which need to be extracted out of webpage
Set doc = IE.document
i = 2 'start row
'Setting the variables
city = Cells(i, 1).Value 'City variable for search
state = Cells(i, 2).Value 'State variable for search
Do Until city = ""
city = Cells(i, 1).Value 'City variable for search
state = Cells(i, 2).Value 'State variable for search
'Search google for state and county
URL = "www.google.com/?safe=active&ssui=on#q=" + city + "+" + state + "+county&safe=active&ssui=on"
IE.navigate URL
IE.Visible = False
Do
DoEvents
Loop Until IE.readyState = READYSTATE_COMPLETE
Application.Wait (Now() + TimeValue("00:00:005")) ' For internal page refresh or loading
dd = doc.getElementsByClassName("_eF")(0).innerText
' Now, trim the name to take the State out
dd = Left(dd, WorksheetFunction.Search(",", dd) - 1)
'set county value
Cells(i, 3).Value = dd
'MsgBox dd
i = i + 1
'Setting the next variables
city = Cells(i, 1).Value 'City variable for search
state = Cells(i, 2).Value 'State variable for search
Loop
MsgBox "The macro has finished running"
End Sub
So far this is what I've got thanks to the help of user @BruceWayne
However sometimes google doesn't prompt up the necessary information if the town doesn't have a county or if it is too obscure. I need the macro to record this and keep going. However, every time I try to make an if statement or a fail check the macro stops and prompts the debug window because it is trying to retrieve doc.getElementsByClassName("_eF")(0).innerText
.
What can I do to check if doc.getElementsByClassName("_eF")(0).innerText
is there and if it is not to record this and keep going?
Thanks!
The methods of retrieving elements from a collection is a zero based index but the
.Length
property is one based. In other words, the first in the collection of a single element is at position0
but the length is1
.I just dealt with this problem recently. I wrote a Function to test if the element I wanted was on the screen. The function is below. I am sure you can modify it for your needs.
To put this in your code, do this: