Sub Calcul()
Dim dur#, RT#, dmax&, t, ub&, resu$(), f$, i&, sinLat#, cosLat#, j&, da#, x$, n&
dur = Timer
RT = 6378.137 'rayon terrestre en km
dmax = Int(Val(Feuil1.[P1])) 'distance maximum retenue en km, cellule à adapter éventuellement
t = Feuil1.[A2:D35250] 'plage à adapter éventuellement
ub = UBound(t)
ReDim resu(1 To ub, 1 To 1)
f = String(Len(CStr(dmax)), "0") & ".0 k\m " 'format des distances
For i = 1 To ub - 1
sinLat = Sin(t(i, 3)): cosLat = Cos(t(i, 3))
For j = i + 1 To ub
da = sinLat * Sin(t(j, 3)) + cosLat * Cos(t(j, 3)) * Cos(t(i, 4) - t(j, 4)) 'cosinus de la distance angulaire
da = Atn(Sqr(Abs(1 - da ^ 2)) / da) 'distance angulaire en radian
If da * RT < dmax Then
x = Format(da * RT, f)
resu(i, 1) = resu(i, 1) & x & t(j, 1) & "#"
resu(j, 1) = resu(j, 1) & x & t(i, 1) & "#"
n = n + 1
End If
Next j, i
Call RAZ
Feuil2.[D2].Resize(ub) = resu 'restitution
Feuil2.Activate
dur = (Timer - dur) / 86400
MsgBox "Nombre de distances retenues " & Format(n, "#,##0") & vbLf & "Durée du calcul " & Minute(dur) & " min " & Second(dur) & " s"
End Sub
Sub Classer()
Dim dur#, i&
dur = Timer
Application.ScreenUpdating = False
With Feuil2.[D2:D35250] 'plage à adapter éventuellement
If InStr(.Cells(1), "#") = 0 Then Exit Sub 'sécurité
.TextToColumns .Cells(1), xlDelimited, Other:=True, OtherChar:="#" 'commande Convertir
For i = 1 To .Count
.Cells(i).Resize(, Columns.Count - 3).Sort .Cells(i), xlAscending, Orientation:=xlLeftToRight 'tri horizontal de chaque ligne
Next i
.Columns(0) = "=COUNTA(" & .Cells(1).Resize(, Columns.Count - 3).Address(0, 0) & ")" 'Nb villes proches
End With
Application.ScreenUpdating = True
dur = (Timer - dur) / 86400
MsgBox "Durée du classement " & Minute(dur) & " min " & Second(dur) & " s"
End Sub
Sub RAZ()
With Feuil2
.Range("C2:C" & Rows.Count).Resize(, Columns.Count - 2).ClearContents
With .UsedRange: End With 'actualise la barre de défilement horizontale
End With
End Sub