How To Calculate Distance in Excel With Google Ma
How To Calculate Distance in Excel With Google Ma
How to Calculate
Distance in Excel
with Google Maps
Written by
Md. Sourov Hossain Mithun
Last updated: Dec 21, 2023
How to Calculate
Distance in Excel with
Google Maps: Using a
User-Defined Function
Here, we’ll find the distance between two cities
such as Las Vegas and Philadelphia using
Google Maps.
Steps:
AoCgFc5qOKVpyHuiGyPBgzDk8RgQnGGMvNq
wcmtxfj7VnHEm-bpqH2GkRpoSJSAD
NOTE
Public Function
Calculate_Distance(startlocati
on As String, endlocation As
String, keyvalue As String) As
Double
Initial_Value =
"https://dev.virtualearth.net/
REST/v1/Routes/DistanceMatrix?
origins="
temp_Value = "&destinations="
Destination_Value =
"&travelMode=driving&o=xml&key
=" & keyvalue &
"&distanceUnit=mi"
Set mitHTTP =
CreateObject("MSXML2.ServerXML
HTTP")
distance =
Round(Round(WorksheetFunction.
FilterXML(mitHTTP.ResponseText
, "//TravelDistance"), 3), 0)
Calculate_Distance = distance
End Function
CODE BREAKDOWN
=Calculate_Distance(C8,C9,C11)
Advantages and
Disadvantages of
Calculating Distance with
Google Maps
Advantages
For a large couple of places, it is quite
feasible because we can use the Fill Handle
tool to copy the formula. That is not possible
in Google Maps
It’s a pretty faster way.
Disadvantages
It can only work with coordinates.
You won’t get the map or route, just you will
get the distance.
It won’t work with the approximate match of
the place names.
Conclusion
I hope the procedures described above will be
good enough to calculate the distance in Excel
with Google Maps. Feel free to ask any questions
in the comment section, and please give me
feedback.
What is ExcelDemy?
ExcelDemy - Learn Excel & Get Excel Solutions Center
provides online Excel training , Excel consultancy services ,
free Excel tutorials, free support , and free Excel Templates
for Excel professionals and businesses. Feel free to contact
us with your Excel problems.
0
Save
12 Comments
Reply
JAN T
Nov 30, 2022 at 3:25 AM
sorry but this can’t work. Your VBA code accepts only 2
arguments, and you use 3 arguments.
full of errors.
Reply
TANJIMA HOSSAIN
Dec 1, 2022 at 12:52 PM
Hello JAN T,
Hope you are doing well. I think by following the below-
stated procedures you can make your code work.
• After going to your VBE window, go to the Tools tab
>> References option.
Public Function
Calculate_Distance(start As
String, dest As String, Alink
As String) As Double
Dim first_Value As String,
second_Value As String,
last_Value As String
Dim mitHTTP As Object
first_Value =
"http://maps.googleapis.com/ma
ps/api/distancematrix/json?
origins="
second_Value =
"&destinations="
last_Value =
"&mode=car&language=pl&sensor=
false&key=" & Alink
Set mitHTTP =
CreateObject("MSXML2.ServerXML
HTTP")
Url = first_Value &
Replace(start, " ", "+") &
second_Value & Replace(dest, "
", "+") & last_Value
mitHTTP.Open "GET", Url, False
mitHTTP.SetRequestHeader
"User-Agent", "Mozilla/4.0
(compatible; MSIE 6.0; Windows
NT 5.0)"
mitHTTP.Send ("")
If InStr(mitHTTP.ResponseText,
"""distance"" : {") = 0 Then
GoTo ErrorHandl
Set mit_reg =
CreateObject("VBScript.RegExp"
): mit_reg.Pattern =
"""value"".*?([0-9]+)":
mit_reg.Global = False
Set mit_matches =
mit_reg.Execute(mitHTTP.Respon
seText)
tmp_Value =
Replace(mit_matches(0).Submit_
matches(0), ".",
Application.International(xlLi
stSeparator))
Calculate_Distance =
CDbl(tmp_Value)
Exit Function
ErrorHandl:
Calculate_Distance = -1
End Function
Regards
Tanjima Hossain
Reply
DEANNA
Jun 15, 2023 at 11:04 AM
Hello DeAnna
Public Function
GetTotalDistance2Locations(
startlocation As String,
endlocation As String,
keyvalue As String) As
Double
Dim Initial_Value As
String, temp_Value As
String, Destination_Value
As String, mitHTTP As
Object, mitUrl As String
Dim distance As Double
Initial_Value =
"https://dev.virtualearth.n
et/REST/v1/Routes/DistanceM
atrix?origins="
temp_Value =
"&destinations="
Destination_Value =
"&travelMode=driving&o=xml&
key=" & keyvalue &
"&distanceUnit=mi"
Set mitHTTP =
CreateObject("MSXML2.Server
XMLHTTP")
distance =
Round(Round(WorksheetFuncti
on.FilterXML(mitHTTP.Respon
seText,
"//TravelDistance"), 3), 0)
GetTotalDistance2Locations
= distance
End Function
API:
AoCgFc5qOKVpyHuiGyPBgzDk8RgQnGGMvNqwcmtxfj
bpqH2GkRpoSJSAD
STEPS:
Select range B3:B4 >> go to Data tab >> click on
Geography.
=GetTotalDistance2Locations(C3,C4,A7)
Download Workbook
Regards
Lutfor Rahman Shimanto
Reply
D KELLY
Jan 27, 2023 at 9:51 PM
Reply
Hi D KELLY,
Thanks for reaching out! I’d be happy to help you with
your code and provide a solution. To solve this issue,
we built a procedure called GetLatLong and a user-
defined function named GetTotalDistance3Locations
using VBA. You can input your desired locations in the
GetLatLong procedure. The model calculates the total
distance for three locations. Please click the link
underneath this section to get a copy of the illustration
workbook.
Download Workbook
Best regards,
Lutfor Rahman Shimanto
(ExcelDemy Team)
Reply
SAMIR
May 16, 2023 at 12:26 AM
Reply
Hello SAMIR
Public Function
GetTotalDistance2Locations(sta
rtlocation As String,
endlocation As String,
keyvalue As String) As Double
Initial_Value =
"https://dev.virtualearth.net/
REST/v1/Routes/DistanceMatrix?
origins="
temp_Value = "&destinations="
Destination_Value =
"&travelMode=driving&o=xml&key
=" & keyvalue &
"&distanceUnit=mi"
Set mitHTTP =
CreateObject("MSXML2.ServerXML
HTTP")
distance =
Round(Round(WorksheetFunction.
FilterXML(mitHTTP.ResponseText
, "//TravelDistance"), 3), 0)
GetTotalDistance2Locations =
distance
End Function
OUTPUT:
WORKBOOK:
SOLUTION WORKBOOK
Regards
Lutfor Rahman Shimanto
Reply
MARCO
Aug 23, 2023 at 4:35 PM
Hello!
I get #NAME? error. May you guys help me out? I’d like
also to get distance in KM instead of miles.
Reply
MD TANVIR RAHMAN
Aug 28, 2023 at 12:11 PM
Regards,
MD Tanvir Rahman
Excel and VBA Content Developer
Exceldemy, Softeko.
Reply
MX A HUSKINS
Oct 2, 2023 at 11:41 PM
Reply
Hello MX A HUSKINS
Regards
Lutfor Rahman Shimanto
LEAVE A REPLY
Name
Website
Submit
About ExcelDemy.com
ExcelDemy is a place where you can learn Excel, and get solutions
to your Excel & Excel VBA-related problems, Data Analysis with
Excel, etc. We provide tips, how to guide, provide online training,
and also provide Excel solutions to your business problems.
Follow Us