I am new to VBA and I have a problem.
I have an excel file with a macro in VBA that allows me to calculate the distance between two cities in kms :
The problem is that if the destination city does not exist the script stops and puts this error that says that the index does not belong to the selection.
When I select "Debug" it highlights this line in yellow:
.Range("C" & i).Value = Split(Split(Txt, "id=""distanciaRuta"">")(1), "</strong>")(0)
How to do that even if the city is not found to continue the execution of the script by leaving a blank in the box?
Option Explicit
Public Const DIST = "http://www.distance2villes.com/recherche?source="
Sub Distance()
Dim lg As Integer, i As Integer
Dim Url As String, Txt As String
With Sheets("Feuil1")
lg = .Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lg
Url = DIST & .Range("A" & i).Value & "&destination=" & .Range("B" & i).Value
With CreateObject("WINHTTP.WinHTTPRequest.5.1")
.Open "GET", Url, False
.send
Txt = .responseText
End With
.Range("C" & i).Value = Split(Split(Txt, "id=""distanciaRuta"">")(1), "</strong>")(0)
Next i
End With
End Sub


On Error Resume Next:.Range....:On Error Goto 0. Note that this is not a proper solution, neither is your extraction of the data. Hopefully, someone more qualified will explain the issues.On Error Resume Next .Range("C" & i).Value = Split(Split(Txt, "id=""distanciaRuta"">")(1), "</strong>")(0) .Range.... : On Error Goto 0On Error Resume Next, 2.line.Range...., and 3. lineOn Error Goto 0.