{"id":15087,"date":"2019-03-20T18:56:02","date_gmt":"2019-03-20T18:56:02","guid":{"rendered":"http:\/\/learnexcelmacro.com\/wp\/?p=15087"},"modified":"2022-08-17T19:12:44","modified_gmt":"2022-08-17T19:12:44","slug":"distance-calculator-in-excel-vba-revised","status":"publish","type":"post","link":"https:\/\/vmlogger.com\/excel\/2019\/03\/distance-calculator-in-excel-vba-revised\/","title":{"rendered":"Distance Calculator in Excel VBA [REVISED]"},"content":{"rendered":"
[et_pb_section fb_built=”1″ _builder_version=”4.17.6″ custom_padding=”0px|0px||0px|false|false” da_disable_devices=”off|off|off” global_colors_info=”{}” da_is_popup=”off” da_exit_intent=”off” da_has_close=”on” da_alt_close=”off” da_dark_close=”off” da_not_modal=”on” da_is_singular=”off” da_with_loader=”off” da_has_shadow=”on”][et_pb_row _builder_version=”4.16″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”][et_pb_column type=”4_4″ _builder_version=”4.16″ custom_padding=”|||” global_colors_info=”{}” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.17.6″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” global_colors_info=”{}”]<\/p>\n
Hi guys, on June 2016, google<\/a> made some changesaround pricing and authentication<\/a> etc. of its FREE APIs.Distance calculator API<\/a> was also affected by those changes. I had written an article long back about how to calculate distance and time taken <\/a> between two places in Excel VBA using google matrix api. In addition to this article, I also built a small Excel tool that calculates the distance and time taken between two places you type it in. It also has an option to choose your mode of transport. It looks something like the below:<\/p>\n Distance Calculator<\/p><\/div><\/figure>\n Ever since the changes have taken place, I am getting many questions from LEM readers that this tool does not work for them anymore.<\/p>\n Therefore, I thought to write this article to explain to you more in detail.<\/p>\n In the previous article, I have simply shared a simple VBA code to call Distance matrix API – where no authentication (API Key) was required…<\/em><\/strong><\/p>\n After that change, the tool I had created and an article I had published was not working. Although I had replied via comments many times, I keep getting this question again and again. Therefore, I decided to write this article to answer all the questions at once.<\/p>\n Now, In order to invoke Distance matrix API from Google, first, you need to get an API key using your google account. For that matter, in order to access any of its APIs, you need to get an API key to authenticate your API calls.<\/p>\n You can follow the simple steps given here in this article from google – https:\/\/developers.google.com\/maps\/documentation\/distance-matrix\/get-api-key<\/a>. Before, you try to call the API from Excel VBA, get this API key for yourself.<\/p>\n As I mentioned previously, it is no longer for free (100%), you will have some limitations by using your FREE account. more details can be found here<\/a><\/p>\n As you have learned in the previous article about how to create different parameters before your make an API call, that remains exactly the same except for the fact that now you need to append &key=YOUR_API_KEY<\/em><\/strong><\/p>\n For example: <\/strong>Here is how a sample URL looks like using an API key<\/p>\n There are Two things: <\/strong><\/p>\n In the code, I have added an exception handling, so that you know what is wrong with the API call. It was my mistake that in my previous article, I did not use any exception handling, therefore it was not clear what exactly the problem is.<\/p>\n Secondly, I have added a placeholder for your API key which you can store in the config sheet and the tool is ready for your own use.<\/p>\n Here is the VBA code to call Google Distance Matrix API and get the distance and time taken :<\/p>\n <\/p>\n [\/et_pb_text][et_pb_cta title=”Distance Calculator Tool” button_url=”\/excel\/wp-content\/downloads\/Google-Distance-Calculator.xlsm” button_text=”Download FREE Workbook” admin_label=”FreeDownload-Sidebar-part-2″ _builder_version=”4.17.6″ _module_preset=”a50a16dd-d05f-4ea2-acab-1468d2e4010e” link_option_url_new_window=”on” button_letter_spacing_hover=”1px” saved_tabs=”all” global_colors_info=”{}” button_one_text_size__hover_enabled=”off” button_two_text_size__hover_enabled=”off” button_one_text_color__hover_enabled=”off” button_two_text_color__hover_enabled=”off” button_one_border_width__hover_enabled=”off” button_two_border_width__hover_enabled=”off” button_one_border_color__hover_enabled=”off” button_two_border_color__hover_enabled=”off” button_one_border_radius__hover_enabled=”off” button_two_border_radius__hover_enabled=”off” button_one_letter_spacing__hover_enabled=”off” button_two_letter_spacing__hover_enabled=”off” button_one_bg_color__hover_enabled=”off” button_two_bg_color__hover_enabled=”off”]<\/p>\n Before you run this excel sheet, you need to get your API key generated from your google account and put that key in the excel sheet at a specified place and then enjoy using it.<\/p>\n [\/et_pb_cta][et_pb_blurb title=”Suggested articles” use_icon=”on” font_icon=”||fa||900″ admin_label=”Blurb” _builder_version=”4.17.6″ _module_preset=”0249c68e-4de8-4f44-84ff-a9b1850785b6″ global_colors_info=”{}”]<\/p>\n [\/et_pb_blurb][\/et_pb_column][\/et_pb_row][\/et_pb_section]<\/p>\n<\/span>","protected":false},"excerpt":{"rendered":" Hi guys, on June 2016, google made some changesaround pricing and authentication etc. of its FREE APIs.Distance calculator API was also affected by those changes. I had written an article long back about how to calculate distance and time taken between two places in Excel VBA using google matrix api. In addition to this article, […]<\/p>\n","protected":false},"author":45,"featured_media":242869,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"on","_et_pb_old_content":"\r\n Hi guys, on June 2016, google<\/a> made some changes around pricing and authentication etc. of <\/a>its FREE APIs. Distance calculator API<\/a> was also affected by those changes.<\/p>\r\n\r\n I had written an article long back about how to calculate distance and time taken between two places in Excel VBA using google matrix api<\/a>. In addition to this article, I had also built a small Excel tool which calculates distance and time taken between two places you type it in. It also has an option to choose your mode of transport. It looks something like below:<\/p>\r\n\r\n Ever since the changes taken place, I am getting many questions from LEM readers that this tool does not work for them anymore.<\/p>\r\n\r\n Therefore, I thought to write this article to explain you more in detail.<\/p>\r\n\r\n In the previous article, i have simply shared a simple VBA code to call Distance matrix API - where no authentication (API Key) was required...<\/em><\/strong><\/p>\r\n\r\n After that changes, the tool I had created and article which I had published were not working. Although, I had replied via comments many time, but I keep getting this question again and again. Therefore, I decided to write this article to answer all the questions at once.<\/p>\r\n\r\n Now, In order to invoke Distance matrix API from google, first you need to get an API key using your google account. For that matter, in order to access any of its API, you need to get an API key to authenticate your API calls.<\/p>\r\n\r\n You can follow the simple steps given here in this article from google - https:\/\/developers.google.com\/maps\/documentation\/distance-matrix\/get-api-key<\/a> . Before, you try to call the API from Excel VBA, get this API key for yourself.<\/p>\r\n\r\nHow to get Google API Key<\/h2>\n
How to use the API key – Excel VBA<\/h2>\n
https:\/\/maps.googleapis.com\/maps\/api\/distancematrix\/xml?origins=Amsterdam&destinations=Utrecht&mode=driving&key=YOUR_API_KEY<\/pre>\n
What is fixed in the Tool<\/h2>\n
VBA to calculate the distance between two places<\/h2>\n
\nFunction getDistanceAndTimeBetweenTwoPlaces() As Variant\n' This function will return an array holding\n' distance in meters and duration in seconds\n Dim googleAPIRequest As XMLHTTP60\n Dim domDoc As DOMDocument60\n Dim xmlNodesList As IXMLDOMNodeList\n Dim status As String\n Dim errorMessage As String\n \n Dim response(1) As Variant 'array to hold distance & duration\n \n On Error GoTo err\n'API URL is formed in excel sheet config using exccel formula\n urlForDistance = Range(\"urlForDistance\").Value\n \n Set googleAPIRequest = New XMLHTTP60\n' invoke the API to get the Distance Matrxi in XML format\n googleAPIRequest.Open \"GET\", urlForDistance, False\n googleAPIRequest.Send\n \n' Get the response XML\n Set domDoc = New DOMDocument60\n domDoc.LoadXML googleAPIRequest.ResponseText\n'using xPath first get the status of the API Call\n status = domDoc.SelectSingleNode(\"\/\/status[1]\").nodeTypedValue\n \n errorMessage = domDoc.Text\n If status = \"OK\" Then\n' Using xPath get the distance\n Set xmlNodesList = domDoc.SelectNodes(\"\/\/distance[1]\/*\")\n response(0) = xmlNodesList(0).Text\n \n' Using xPath get the duration\n Set xmlNodesList = domDoc.SelectNodes(\"\/\/duration[1]\/*\")\n response(1) = xmlNodesList(0).Text\n \n' Return response with distance and duration in array\n getDistanceAndTimeBetweenTwoPlaces = response\n \n' release memory\n Set xmlNodesList = Nothing\n Else\n \n MsgBox errorMessage\n \n End If\nerr:\n Set domDoc = Nothing\n Set googleAPIRequest = Nothing\nEnd Function\n<\/code><\/pre>\n
Note:<\/h3>\n
\n
<\/figure>\r\n\r\n
How to get Google API Key<\/h2>\r\n\r\n