VBA - Need help editing a GPS related VBA for an extra function (Code inside)

Kenny27

New Member
Joined
Jun 27, 2016
Messages
23
Basically, a user called "Ombir" helped me create a code which automatically lets me get the GPS co-ordinates (Latitude and Longitude) for a certain street address. So, you just type in the street address and poof - two new columns of GPS co-ordinates. Here's the code:

Code:
[/COLOR][COLOR=#333333]Sub Geocoding()Dim rng     As Range[/COLOR]
Dim lat     As String
Dim lng     As String
Dim ndxla1  As Long
Dim ndxla2  As Long
Dim ndxlo1  As Long
Dim ndxlo2  As Long
Dim url     As String
Dim resp    As String
Dim req     As Object
Const api   As String = "Paste Your Api Key Here"


Application.ScreenUpdating = False


Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)


rng.Replace What:=" ", Replacement:="+"


For Each cell In rng
    url = "https://maps.googleapis.com/maps/api/geocode/json?address=" & cell.Value & "&key=" & api
    
    req.Open "GET", url, False
    req.Send: resp = req.ResponseText
     
    If InStr(resp, "ZERO_RESULTS") = 0 Then
    
        ndxla1 = InStr(resp, """" & "lat" & """") + 8
        ndxlo1 = InStr(resp, """" & "lng" & """") + 8
        
        ndxla2 = InStr(ndxla1, resp, ",") - ndxla1
        ndxlo2 = InStr(ndxlo1, resp, ",") - ndxlo1 - 1
        
        lat = Mid$(resp, ndxla1, ndxla2)
        lng = Mid$(resp, ndxlo1, ndxlo2)
        
        cell.Offset(, 1) = lat: cell.Offset(, 2) = lng
    End If
Next
rng.Replace What:="+", Replacement:=" "
Application.ScreenUpdating = True
MsgBox "Geocoding Completed"
 [COLOR=#333333]End Sub[/COLOR][COLOR=#333333]

Now, google's GPS site gives out 4 result quality types for the address you've put in:

1. ROOFTOP
2. RANGE_INTERPOLATED
3. GEOMETRIC_CENTER
4. APPROXIMATE

All of which can be found on this batch geocoding app by ticking the "Result quality" box: https://www.doogal.co.uk/BatchGeocoding.php

Now, I basically want all results which aren't "Rooftop" to simply appear as blank spaces or appear as "FAILED" or whatnot. I basically just want the "rooftop" results to appear out of all 4 result types.

In all honesty I have NO idea where to look for the result types so I was hoping someone could help me.
P.S: I don't want to seem pushy, at all - this I really hope this is not too much to ask for... If it is then by all means, I'd be 120% happy with the request above.

If there'd be somewhat of option in the VBA to either enable or disable the 4 types of quality results (As in, the request above for example would basically be:

1. ROOFTOP - enabled.
2. RANGE_INTERPOLATED - disabled.
3. GEOMETRIC_CENTER - disabled.
4. APPROXIMATE - disabled.

I mainly want this because I might get a request to expand the search radius or in other words - expand to Range Interpolated as well in the near future, so instead of making another request or flopping in general, that would be great.


Thanks in advance!

Kenny27
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
what does an output text string look like (exact once run)
 
Upvote 0
This is how it looks before I run the VBA:

Before.png



And this is how it looks after I run it:
After.png



So, it currently just pasts the co-ordinates for all 4 types of result quality and when an address doesn't exist or Google's GPS system can't find it - it simply leaves a blank square, which is good for me. I was wondering if something can be done to leave the 3 quality types which aren't "Rooftop" blank and just keep the "Rooftop" co-ordinates.
 
Upvote 0
for small amounts i would just sort by B, and delete the blanks
 
Upvote 0
Sorry I didn't reply for awhile - didn't have access to my user because I wasn't at work.

Anyway, I think I didn't explain the issue well enough. Basically, I currently get an address no matter which quality I choose, here's a picture of what it looks like to run my addresses through the batch geocoding site you've suggested:

1.png


But what I want to do is just keep around the Rooftop quality results - leaving the Approximate, Geometric Center and Range Interpolated results blank, or showing me some sort of failed message in those specific address' cells. Basically, I want to keep these results:

2.png

I have thousands of lines to complete so I'd really appreciate your help on this - If it was up to a thousand then I'd just throw everything into the site but yeah, they keep flowing in so it's basically endless.
 
Upvote 0
Sorry I didn't reply for awhile - didn't have access to my user because I wasn't at work.

Anyway, I think I didn't explain the issue well enough. Basically, I currently get an address no matter which quality I choose, here's a picture of what it looks like to run my addresses through the batch geocoding site you've suggested:

1.png


But what I want to do is just keep around the Rooftop quality results - leaving the Approximate, Geometric Center and Range Interpolated results blank, or showing me some sort of failed message in those specific address' cells. Basically, I want to keep these results:

2.png

I have thousands of lines to complete so I'd really appreciate your help on this - If it was up to a thousand then I'd just throw everything into the site but yeah, they keep flowing in so it's basically endless.

Try this Kenny,

Code:
Sub Geocoding()
Dim i       As Long
Dim rng     As Range
Dim lat     As String
Dim lng     As String
Dim ndxla1  As Long
Dim ndxla2  As Long
Dim ndxlo1  As Long
Dim ndxlo2  As Long
Dim url     As String
Dim resp    As String
Dim req     As Object
Const api   As String = "Paste your API key here"

Application.ScreenUpdating = False

Set req = CreateObject("WinHttp.WinHttpRequest.5.1")
Set rng = Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)

rng.Replace What:=" ", Replacement:="+"

For Each cell In rng
    url = "https://maps.googleapis.com/maps/api/geocode/json?address=" & cell.Value & "&key=" & api
    
    req.Open "GET", url, False
    req.Send: resp = req.ResponseText
     
    If InStr(resp, "ZERO_RESULTS") = 0 And InStr(resp, "ROOFTOP") > 0 Then
        
        rt = InStr(resp, "ROOFTOP")
        
        ndxla1 = InStr(rt, resp, """" & "lat" & """") + 8
        ndxlo1 = InStr(rt, resp, """" & "lng" & """") + 8
        
        ndxla2 = InStr(ndxla1, resp, ",") - ndxla1
        ndxlo2 = InStr(ndxlo1, resp, ",") - ndxlo1 - 1
        
        lat = Mid$(resp, ndxla1, ndxla2)
        lng = Mid$(resp, ndxlo1, ndxlo2)
        
        cell.Offset(, 1) = lat: cell.Offset(, 2) = lng
    End If
Next
rng.Replace What:="+", Replacement:=" "
Application.ScreenUpdating = True
MsgBox "Geocoding Completed"
End Sub

Regards,
Ombir
 
Last edited:
Upvote 0
If you have thousands of addresses then I would also suggest you to run multiple Instances of Excel application(4-5) by using command excel.exe /x in Run Window.

Then you can divide your data in each instance of Excel and will able to run vba code in parallel so that maximum internet bandwidth can be used.
 
Upvote 0
You're a life saver, I swear... Like, seriously - you can't imagine how much you helped me - twice!

I seriously owe you. So, again - thanks!!!

P.S: Great tip, already trying it!
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top