Private Sub CommandButton1_Click()
Dim l As Integer, tablo, i As Integer, x As Double, y As Double, dmin As Double, j As Integer, d As Double
Application.ScreenUpdating = False
Cells.ClearContents 'RAZ
l = Int((100 - 50 + 1) * Rnd() + 50)
[A1:E1] = Array("N", "X", "Y", "Nearest N", "Nearest D")
tablo = [A2].Resize(l, 5) 'matrix, faster
For i = 1 To l
tablo(i, 1) = i
tablo(i, 2) = Rnd() * 100
tablo(i, 3) = Rnd() * 100
Next i
For i = 1 To l
x = tablo(i, 2): y = tablo(i, 3)
dmin = 150
For j = 1 To l
If j <> i Then
d = Sqr((tablo(j, 2) - x) ^ 2 + (tablo(j, 3) - y) ^ 2)
If d < dmin Then dmin = d: tablo(i, 4) = tablo(j, 1): tablo(i, 5) = d
End If
Next j, i
[A2].Resize(l, 5) = tablo 'restitution
End Sub