I have an Excel file that allows me to calculate the distances between two cities by specifying the country in which the destination city is located so that it searches in the right country and avoids making mistakes with the same city names in different countries.
I noticed a problem is that
- sometimes it does not find certain cities, especially in France and so it puts "
663 km" each time because it only looks for "FR". - and for some cities like Rome or Paris and Marseille it does not find anything. For Rome it's also due to spelling (
Roma) and for Paris and Marseille it's because the added district number it doesn't take into account:
City of departure City of destination Destination country code Distance (km) Correct names
Soorts-Hossegor PONT L ABBE FR 663 Pont-l'Abbé
Soorts-Hossegor PONT L ABBE FR 663 Dolus-D'Oléron
Soorts-Hossegor DOLUS D OLERON FR 663 Saint-Pierre-d'Oléron
Soorts-Hossegor PONT L ABBE FR 663 Rome
Soorts-Hossegor DOLUS D OLERON FR 663 Paris
Soorts-Hossegor ST PIERRE D OLERON FR 663 Marseille
Soorts-Hossegor NAPLES IT 1740
Soorts-Hossegor ST PIERRE D OLERON FR 663
Soorts-Hossegor DAX FR 40
Soorts-Hossegor ST PIERRE D OLERON FR 663
Soorts-Hossegor PONT L ABBE FR 663
Soorts-Hossegor BREST FR 817
Soorts-Hossegor ST PIERRE D OLERON FR 663
Soorts-Hossegor PONT L ABBE FR 663
Soorts-Hossegor ST PIERRE D OLERON FR 663
Soorts-Hossegor ST JEAN D AULPS FR 663
Soorts-Hossegor ROMA TRIGORIA IT
Soorts-Hossegor PARIS 11 FR
Soorts-Hossegor MARSEILLE 03 FR
I have added a column in my file with the names of the cities that are problematic, spelled correctly.
I would like to know if it is possible by running my script to search in this column and to correct the cities that cause problems? This last column could be changed as and when I find the names of cities that cause problems.
Option Explicit
Sub Distance()
Const DIST1 As String = "http://www.distance2villes.com/recherche?source="
Const DIST2 As String = "&destination="
Const DIST3 As String = "distanciaRuta"
Const wsName As String = "Feuil1"
'Dim w As Object: Set w = CreateObject("WINHTTP.WinHTTPRequest.5.1")
Dim w As Object: Set w = CreateObject("MSXML2.XMLHTTP")
Dim h As Object: Set h = CreateObject("htmlfile")
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets(wsName)
Dim rg As Range
Set rg = ws.Range("A2", ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(, 2))
Dim Data As Variant: Data = rg.Value
Dim isFound As Boolean: isFound = True
Dim i As Long
Dim Url As String
Dim S As String
For i = 1 To UBound(Data, 1)
If Len(Data(i, 1)) > 0 And Len(Data(i, 2)) > 0 Then
Url = DIST1 & Data(i, 1) & DIST2 & Data(i, 2) & "%20" & Data(i, 3)
w.Open "GET", Url, False
w.Send
h.body.innerHTML = w.responseText
On Error GoTo NotFoundError
S = h.getElementById(DIST3).innerText
On Error GoTo 0
If isFound Then
Data(i, 1) = Replace(Left(S, Len(S) - 3), ",", "")
Else
Data(i, 1) = ""
isFound = True
End If
Else
Data(i, 1) = ""
End If
Next
rg.Columns(1).Offset(, 3).Value = Data
Exit Sub
NotFoundError:
isFound = False
Resume Next
End Sub