Option Explicit '1)
Private Sub Worksheet_SelectionChange(ByVal Target As Range) '2)
Liste ActiveCell '3)
End Sub
Private Sub Worksheet_Change(ByVal Target As Range) '4)
Liste Target.Cells(1, 1) '5)
End Sub
Sub Liste(Target As Range) '6)
Static t As Single '7)
Dim test1 As Byte, test2 As Long, n As Long, CP As Variant
Dim txt As String, deb As Range, fin As Range
If Abs(Timer - t) < 0.1 Then Exit Sub '8)
With Sheets("villes")
test1 = InStr("1-2-3", Len(Target)) '9)
test2 = Application.CountIf(.[E2:E65536], Target & "*") '10)
If Not Intersect(Target, [A2:A65536]) Is Nothing Then '11)
t = Timer '12)
Target.Activate '13)
n = Application.CountIf(.[E2:E65536], Target) '14
If n = 1 Then CP = Application.VLookup(Target, .[E2:F65536], 2, 0) '15)
Target.Offset(, 1) = CP '16)
If n > 1 Then MsgBox "Plusieurs villes portent ce nom, recherchez le code postal...", 48 '17)
If test1 * test2 Then SendKeys "%{DOWN}" '18)
End If
txt = IIf(test1 * test2, Target & "*", "*") '19)
With .[E2:E65536]
Set deb = .Find(txt, LookIn:=xlValues, LookAt:=xlWhole) '20)
Set fin = .Find(txt, SearchDirection:=xlPrevious) '21)
End With
.Range(deb, fin).Name = "Liste" '22)
End With
t = Timer '12)
End Sub