{"id":2135,"date":"2016-01-24T19:29:35","date_gmt":"2016-01-24T19:29:35","guid":{"rendered":"https:\/\/grindgis.com\/?p=2135"},"modified":"2018-04-05T14:30:00","modified_gmt":"2018-04-05T14:30:00","slug":"geocoding-excel-and-google","status":"publish","type":"post","link":"https:\/\/grindgis.com\/?p=2135","title":{"rendered":"Geocoding using Microsoft Excel 2013 and Google Maps API"},"content":{"rendered":"<p style=\"text-align: justify;\">Geocoding is the process of converting addresses to the x and y coordinate or longitude and latitude. There are various option available in the market to perform geocoding, some are paid and some are free. You can use <a href=\"https:\/\/developers.google.com\/maps\/documentation\/geocoding\/intro\" target=\"_blank\" rel=\"noopener\">The\u00a0Google Maps Geocoding API<\/a> to retrieve the longitude and latitude for free. Later you can convert this longitude and latitude to your desire coordinate values, there are number of online tools for doing that. So it will be hassle to perform each addresses one at time in google, for that you can use Excel software to perform large number of addresses. You create excel function to perform the geocoding for multiple addresses.<\/p>\n<p style=\"text-align: justify;\">For this task I am using Microsoft Excel 2013, you can use 2007 or 2010 version. First task is to turn on the Developer tab. To find this tab in 2007 or 2010 version you have to play around.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 50pt;\"><strong>1.<\/strong><\/span> Go to the Microsoft <strong>File &gt; Options &gt; Customize Ribbon <\/strong>and check<strong> Developer<br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Developer tab\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus1.png\" alt=\"\" width=\"626\" height=\"308\" \/><strong><br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong>You will notice Developer tab is added on the ribbon.<br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Developer ribbon\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus2.png\" alt=\"\" width=\"626\" height=\"92\" \/><strong><br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 50pt;\"><strong>2. <\/strong><\/span>Now click the Visual Basic button on the far left corner which will open the new window. Now we want a new module to insert which will store our new function. For that click on <strong>Insert &gt; New Module<\/strong><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"module option\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus3.png\" alt=\"\" width=\"460\" height=\"185\" \/><strong><br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><strong>Copy the below code and paste into the empty area (code source: policeanalyst.com)<br \/>\n<\/strong><\/p>\n<div>\n<table style=\"border-collapse: collapse; background: #d9d9d9;\" border=\"0\">\n<colgroup>\n<col style=\"width: 623px;\" \/><\/colgroup>\n<tbody valign=\"top\">\n<tr>\n<td style=\"padding-left: 7px; padding-right: 7px; border: solid 1.0pt;\">\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>Function MyGeocode(address As String) As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim strAddress As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim strQuery As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim strLatitude As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim strLongitude As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  strAddress = URLEncode(address)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  'Assemble the query string\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  strQuery = \"http:\/\/maps.googleapis.com\/maps\/api\/geocode\/xml?\"\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  strQuery = strQuery &amp; \"address=\" &amp; strAddress \r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  strQuery = strQuery &amp; \"&amp;sensor=false\"\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  'define XML and HTTP components\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim googleResult As New MSXML2.DOMDocument\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim googleService As New MSXML2.XMLHTTP\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim oNodes As MSXML2.IXMLDOMNodeList\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim oNode As MSXML2.IXMLDOMNode\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  'create HTTP request to query URL - make sure to have\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  'that last \"False\" there for synchronous operation\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  googleService.Open \"GET\", strQuery, False\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  googleService.send\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  googleResult.LoadXML (googleService.responseText)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Set oNodes = googleResult.getElementsByTagName(\"geometry\")\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  If oNodes.Length = 1 Then\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    For Each oNode In oNodes\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      strLatitude = oNode.ChildNodes(0).ChildNodes(0).Text\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      strLongitude = oNode.ChildNodes(0).ChildNodes(1).Text\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      MyGeocode = strLatitude &amp; \",\" &amp; strLongitude\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    Next oNode\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Else\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    MyGeocode = \"Not Found (try again, you may have done too many too fast)\"\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  End If\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>End Function\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  Dim StringLen As Long: StringLen = Len(StringVal)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  If StringLen&gt;0 Then\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    ReDim result(StringLen) As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    Dim i As Long, CharCode As Integer\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    Dim Char As String, Space As String\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    If SpaceAsPlus Then Space = \"+\" Else Space = \"%20\"\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    For i = 1 To StringLen\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Char = Mid$(StringVal, i, 1)  \r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      CharCode = Asc(Char)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Select Case CharCode\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>        result(i) = Char\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Case 32\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>        result(i) = Space\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Case 0 To 15\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>        result(i) = \"%0\" &amp; Hex(CharCode)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      Case Else\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>        result(i) = \"%\" &amp; Hex(CharCode)\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>      End Select\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    Next i\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>    URLEncode = Join(result, \"\")\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>  End If\r\n<\/em><\/span><\/code><\/pre>\n<pre><code><span style=\"font-family: font-size: 11pt;\"><em>End Function<\/em><\/span><\/code><\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"text-align: justify;\">This code make use of Microsoft&#8217;s XML library, so you have to add references to it. To add the reference go to <strong>Tools &gt; References<br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\">You will see list of references with the check box, select the<strong> Microsoft XML v6.0, <\/strong>after that click Ok button.<\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"microsoft XML, v6.0\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus4.png\" alt=\"\" width=\"452\" height=\"368\" \/><\/p>\n<p style=\"text-align: justify;\">Finally save your Visual basic window by pressing save button. Then from the drop down menu of Save as type select <strong>Excel Add-In, <\/strong>assign the file name of your desire. Once the file in saved in &#8220;*.xlam&#8221; extension you can open any new document and use the function. Close all the open excel files and open the new one.<\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 50pt;\"><strong>3. <\/strong><\/span>Open the new excel file and then go to file &gt; Options &gt; Add-Inns. From the Manage drop down box select the <strong>Excel-Add Ins.<br \/>\n<\/strong><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Excel Add-Inns\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus5.png\" alt=\"\" width=\"626\" height=\"511\" \/><\/p>\n<p style=\"text-align: justify;\">After selecting then press the <strong>GO<\/strong> button, this will list all the saved Add-ins files, as I have saved as Book1. Xlam I will check that box. Then press Ok after selecting,<\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Excel sheet check in\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus6.png\" alt=\"\" width=\"294\" height=\"259\" \/><\/p>\n<p style=\"text-align: justify;\"><span style=\"font-size: 50pt;\"><strong>4. <\/strong><\/span>As everything is done to perform the geocode, we are going to use the function to retrieve the longitude and latitude value. Let us suppose I have address <span style=\"color: black;\"><strong>65 Hawthorn way, Olds, Alberta, Canada<\/strong> then you type that value in excel and use the function <strong>MyGeocode<\/strong> (as you type <strong>my<\/strong> it will show function on the box).<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"Mygeocode function\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus7.png\" alt=\"\" width=\"447\" height=\"54\" \/><span style=\"color: black;\"><br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black;\">It will take few seconds to run the function because it has to pull the information from the google server and then result will display.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"function retrieve value\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus8.png\" alt=\"\" width=\"447\" height=\"57\" \/><span style=\"color: black;\"><br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black;\">Now if you have number of addresses and you want to run the geocode then this function will help you. Just keep in mind few things while using this function, after the execution process is done then you can copy and paste the values because each time you open the file then it will processing each cells which might take up few minutes to perform it.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black;\">Second things as it is free they will not allow you to do perform thousands of geocode at once, so best way is to have a 10 seconds break between addresses. If Google stop you for retrieving large number of addresses, you will see error saying <strong>&#8220;Not Found (try again, you may have done too many too fast)&#8221;.<\/strong> See below example, I have tried few number of addressee without following 10 seconds rule.<br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" title=\"error message\" src=\"https:\/\/grindgis.com\/wp-content\/uploads\/2016\/01\/012416_1910_Geocodingus9.png\" alt=\"\" width=\"521\" height=\"101\" \/><span style=\"color: black;\"><br \/>\n<\/span><\/p>\n<p style=\"text-align: justify;\"><span style=\"color: black;\">I hope you liked this tutorial, please comment if you have any question regarding this.<br \/>\n<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Geocoding is the process of converting addresses to the x and y coordinate or longitude and latitude. There are various option available in the market to perform geocoding, some are paid and some are free. You can use The\u00a0Google Maps Geocoding API to retrieve the longitude and latitude for free. Later you can convert this [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2140,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[46,383,382,381,380],"class_list":["post-2135","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microsoft-excel","tag-46","tag-excel-and-geocode","tag-geocoding","tag-google-api","tag-microsoft-excel","entry","has-media"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts\/2135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2135"}],"version-history":[{"count":0,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/posts\/2135\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=\/wp\/v2\/media\/2140"}],"wp:attachment":[{"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/grindgis.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}