VBA to get distance between two Postcodes using Bing API giving 400 error on certain lines.

SimonPostcode

New Member
Joined
Nov 16, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
This is the code

Option Explicit

Const BaseURL As String = "http://dev.virtualearth.net/REST/V1/Routes/Driving?wp.0="
Const APIKey As String = "tsNlc2fsKN0BN5fcfXdU~UEPmyF6E7-PVJQzuaM_o5w~Am8-J3IwQG7V_ZGHT02VjmfpEq7jx697-0UlLiqzDrIiMLaK5d3UIR24RpirTGyW"
Private Const READYSTATE_COMPLETE As Long = 4

Public Sub GetDistances(Addresses As Range)
Dim Server As Object
Dim ServerItem As Variant
Dim Servers As Object
Dim Cell As Range
Dim URL As String

Set Servers = CreateObject("Scripting.Dictionary")

'Send all the requests up front, but don't wait for them to complete
For Each Cell In Addresses
'See here: Driving Route Example - Bing Maps for more details on this api
URL = BaseURL & Cell & "&wp.1=" & Cell.Offset(0, 1) & "&key=" & APIKey & "&DistanceUnit=mi&DurationUnit=min&output=xml"
Set Server = CreateObject("MSXML2.ServerXMLHTTP")
Server.Open "GET", URL, True 'Last param will make request async
Server.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
Server.send ("")
Servers.Add Cell.Address, Server
Next

'Iterate each XML request sent to see if done
For Each ServerItem In Servers.Keys()
Set Server = Servers(ServerItem)

While Server.readyState <> READYSTATE_COMPLETE
DoEvents
Wend

'Parse result
If Server.Status = 200 Then
'Add result to the sheet to an offsetting column
Addresses.Parent.Range(ServerItem).Offset(0, 2) = WorksheetFunction.FilterXML(Server.ResponseText, "/Response/ResourceSets/ResourceSet/Resources/Route/TravelDistance")
Addresses.Parent.Range(ServerItem).Offset(0, 3) = WorksheetFunction.FilterXML(Server.ResponseText, "/Response/ResourceSets/ResourceSet/Resources/Route/TravelDuration") / 60
'You can also return the lat/long from this request, see --> Driving Route Example - Bing Maps
End If
Next

End Sub

Sub ProcessData()
Dim myRng As Range
Dim t As Double
t = Timer
Set myRng = ThisWorkbook.Sheets(1).Range("A1:a20")
GetDistances myRng
Debug.Print Timer - t
End Sub

The code does pull through the distance on some lines but others give an error
1668617342679.png


First line pulls both distance in miles and time in minutes with no issue but the second just gives an error.

1668617361938.png


Any help would be apperciated.

Thanks.
 

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.

Forum statistics

Threads
1,215,094
Messages
6,123,071
Members
449,092
Latest member
ipruravindra

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