I would like to ultimately be able to get the mileage distance from google maps by entering both a location and destination in Excel. If the mileage could be outputted back into Excel. I am not sure of how to go about this, since i'm new to all of this, i have not done any programming in a good 10 years. If I could possibly get some help on this and pointed in the right direction.
If it is possible to get the mileage outputted. I have already setup all my calculations i would need to be used with the mileage.
I am trying to get the distance from mapquest after I enter in the To and From location in Excel. First I enter in the information listed below.
Cell A1 - Address
B1 - City
C1 = State
D1 - Address
E1 - City
F1 - State
I type in the information in excel and run the program but keep getting a compiling error. Can anyone please help? The code I have is listed below.
Public Function GetDistance(startAddr As String, startCity As String, _
startState As String, startZip As String, endAddr As String, _
endCity As String, endState As String, endZip As String) As String
Dim sURL As String
Dim appIE As InternetExplorer
Dim regex As RegExp, Regmatch As MatchCollection
Dim BodyTxt As String
Dim GetFirstPos As Long
sURL = "http://www.mapquest.com/maps?1c=" & Replace(startCity, " ", "+")
sURL = sURL & "&1s=" & startState & "&1a=" & Replace(startAddr, " ", "+")
sURL = sURL & "&1z=" & startZip & "&2c=" & endCity & "&2s=" & endState
sURL = sURL & "&2a=" & Replace(endAddr, " ", "+") & "&2z=" & endZip
Set appIE = New InternetExplorer
'Set appIE = CreateObject("Internetexplorer.application")
appIE.navigate sURL
appIE.Visible = True
Do
DoEvents
Loop Until appIE.readyState = READYSTATE_COMPLETE
appIE.Refresh
Set regex = New RegExp
With regex
.Pattern = "Total Estimated Distance"
.MultiLine = False
End With
BodyTxt = appIE.document.Body.innerText
Set Regmatch = regex.Execute(BodyTxt)
If Regmatch.Count > 0 Then
GetFirstPos = WorksheetFunction.Find("Total Estimated Distance", BodyTxt, 1)
GetDistance = Mid$(BodyTxt, GetFirstPos, 30)
Else
GetDistance = "Address Error, fix and try again"
End If
appIE.Quit
Set appIE = Nothing
Set regex = Nothing
Set Regmatch = Nothing
End Function
Bookmarks