Dim tablo 'mémorise la variable
Private Sub ComboBox1_Change() 'Ville
Dim RT$, daMax, i As Variant, Lat, Lon, sinLat, cosLat, j&, da, a(), n&, resu()
RT = 6378.137 'rayon terrestre en km
daMax = Val(ComboBox2) / RT 'distance angulaire max
i = Application.Match(ComboBox1, Columns(1), 0)
If IsNumeric(i) And UBound(tablo) > 2 Then
Lat = tablo(i, 3): Lon = tablo(i, 4): sinLat = Sin(Lat): cosLat = Cos(Lat)
For j = 2 To UBound(tablo)
If j <> i Then
da = sinLat * Sin(tablo(j, 3)) + cosLat * Cos(tablo(j, 3)) * Cos(Lon - tablo(j, 4)) 'cosinus de la distance angulaire
da = Atn(Sqr(Abs(1 - da ^ 2)) / da) 'distance angulaire en radian
If da <= daMax Then
ReDim Preserve a(1, n) 'base 0
a(0, n) = tablo(j, 1)
a(1, n) = RT * da
n = n + 1
End If
End If
Next j
End If
If n Then
tri a, 0, n - 1
ReDim resu(n - 1, 1)
For j = 0 To n - 1
resu(j, 0) = a(0, j)
resu(j, 1) = Format(a(1, j), "0.0 k\m")
Next j
ListBox1.List = resu
ListBox1.ListIndex = -1
Else
ListBox1.Clear
End If
End Sub
Private Sub ComboBox2_Change() 'Dmax
ComboBox1_Change
End Sub
Private Sub UserForm_Initialize()
With [A1].CurrentRegion.Resize(, 4)
tablo = .Value
If .Rows.Count > 1 Then ComboBox1.List = .Offset(1).Resize(.Rows.Count - 1).Value
End With
ComboBox2.List = Array("10 km", "20 km", "30 km", "40 km", "50 km", "100 km", "150 km")
End Sub
Sub tri(a, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = a(1, (gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(1, g) < ref: g = g + 1: Loop
Do While ref < a(1, d): d = d - 1: Loop
If g <= d Then
temp = a(1, g): a(1, g) = a(1, d): a(1, d) = temp
temp = a(0, g): a(0, g) = a(0, d): a(0, d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(a, g, droi)
If gauc < d Then Call tri(a, gauc, d)
End Sub