Private Sub TextBox1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim Plage As Range
Dim Cel As Range
Dim Col As Integer
'ajoute les espaces automatiquement
If KeyCode <> 8 Then
If Len(TextBox1.Text) = 2 Then TextBox1.Text = TextBox1.Text & " "
If Len(TextBox1.Text) = 5 Then TextBox1.Text = TextBox1.Text & " "
If Len(TextBox1.Text) = 8 Then TextBox1.Text = TextBox1.Text & " "
If Len(TextBox1.Text) = 11 Then TextBox1.Text = TextBox1.Text & " "
End If
'empêche d'aller plus loin, une fois le dernier n° inscrit et lance la recherche
If Len(TextBox1.Text) = 14 Then
If KeyCode <> 8 Then KeyCode = 13
End If
'si appui sur la touche Entrée
If KeyCode = 13 Then
'défini la colonne de recherche (noms colonne A ou numéros colonne B)
If OptionButton2.Value = True Then Col = 1 Else Col = 2
'défini la plage de recherche
With Worksheets("Feuil1")
Set Plage = .Range(.Cells(2, Col), .Cells(.Rows.Count, Col).End(xlUp))
End With
'effectue la recherche
Set Cel = Plage.Find(TextBox1.Text, , xlValues, xlPart)
'si trouvé...
If Not Cel Is Nothing Then
'indique le résultat
If Col = 1 Then
TextBox2.Text = Cel.Offset(, 1).Value
Else
TextBox2.Text = Cel.Offset(, -1).Value
End If
'sinon...
Else
'affiche le message
TextBox2.Text = "Pas trouvé !"
End If
End If
End Sub