XL 2019 Format textbox pour immatriculation voiture

natorp

XLDnaute Barbatruc
Bonjour à toutes et tous

J'aimerais tester la saisie de l'immatriculation du Textbox1
Si le premier caractère saisi est un chiffre alors le format sera : ####-##-##
Si le premier caractère saisi est une lettre alors le format sera : ##-###-##

Est-ce possible, s'il vous plait ?

Cordialement, Gérard
 

Pièces jointes

  • Classeur1.xlsm
    23.4 KB · Affichages: 7
Solution
Bonjour natorp,

Teste cette macro dans l'UserForm :
VB:
Private Sub TextBox1_Change()
Dim x$
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) >= 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) >= 7 Then x = Application.Replace(x, 8, 0, "-")
    x = Left(x, 10)
Else
    If Len(x) >= 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) >= 6 Then x = Application.Replace(x, 7, 0, "-")
    x = Left(x, 9)
End If
TextBox1 = UCase(x)
End Sub
A+

natorp

XLDnaute Barbatruc
J'ai testé ça mais ça ne fonctionne pas :
VB:
Private Sub TextBox1_Change()
If Not TextBox1 Like "[A-Z][A-Z][0-9][0-9][0-9][A-Z][A-Z]" Then
Me.TextBox1.Value = Format(TextBox1.Value, "####""-""##""-""##")
Else
Me.TextBox1.Value = Format(TextBox1.Value, "##""-""###""-""##")
End If
End Sub
 

job75

XLDnaute Barbatruc
Bonjour natorp,

Teste cette macro dans l'UserForm :
VB:
Private Sub TextBox1_Change()
Dim x$
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) >= 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) >= 7 Then x = Application.Replace(x, 8, 0, "-")
    x = Left(x, 10)
Else
    If Len(x) >= 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) >= 6 Then x = Application.Replace(x, 7, 0, "-")
    x = Left(x, 9)
End If
TextBox1 = UCase(x)
End Sub
A+
 

natorp

XLDnaute Barbatruc
Bonjour natorp,

Teste cette macro dans l'UserForm :
VB:
Private Sub TextBox1_Change()
Dim x$
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) >= 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) >= 7 Then x = Application.Replace(x, 8, 0, "-")
    x = Left(x, 10)
Else
    If Len(x) >= 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) >= 6 Then x = Application.Replace(x, 7, 0, "-")
    x = Left(x, 9)
End If
TextBox1 = UCase(x)
End Sub
A+
C'est parfait ! merci beaucoup
Bonne journée, Gérard
 

job75

XLDnaute Barbatruc
Teste aussi ce code avec une MsgBox si la succession des chiffres et des lettres est incorrecte :
VB:
Private Sub TextBox1_Change()
Dim x$, mes
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) > 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) > 7 Then x = Application.Replace(x, 8, 0, "-")
    x = UCase(Left(x, 10))
    If Len(x) = 10 Then If Not x Like "####-[A-Z][A-Z]-##" Then mes = "Le format doit être 0000-AA-00"
Else
    If Len(x) > 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) > 6 Then x = Application.Replace(x, 7, 0, "-")
    x = UCase(Left(x, 9))
    If Len(x) = 9 Then If Not x Like "[A-Z][A-Z]-###-[A-Z][A-Z]" Then mes = "Le format doit être AA-000-AA"
End If
TextBox1 = x
If mes <> "" Then MsgBox mes
End Sub
 

natorp

XLDnaute Barbatruc
Teste aussi ce code avec une MsgBox si la succession des chiffres et des lettres est incorrecte :
VB:
Private Sub TextBox1_Change()
Dim x$, mes
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) > 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) > 7 Then x = Application.Replace(x, 8, 0, "-")
    x = UCase(Left(x, 10))
    If Len(x) = 10 Then If Not x Like "####-[A-Z][A-Z]-##" Then mes = "Le format doit être 0000-AA-00"
Else
    If Len(x) > 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) > 6 Then x = Application.Replace(x, 7, 0, "-")
    x = UCase(Left(x, 9))
    If Len(x) = 9 Then If Not x Like "[A-Z][A-Z]-###-[A-Z][A-Z]" Then mes = "Le format doit être AA-000-AA"
End If
TextBox1 = x
If mes <> "" Then MsgBox mes
End Sub
Merci
 

natorp

XLDnaute Barbatruc
Teste aussi ce code avec une MsgBox si la succession des chiffres et des lettres est incorrecte :
VB:
Private Sub TextBox1_Change()
Dim x$, mes
x = TextBox1
x = Replace(x, "-", "")
If IsNumeric(Left(x, 1)) Then
    If Len(x) > 4 Then x = Application.Replace(x, 5, 0, "-")
    If Len(x) > 7 Then x = Application.Replace(x, 8, 0, "-")
    x = UCase(Left(x, 10))
    If Len(x) = 10 Then If Not x Like "####-[A-Z][A-Z]-##" Then mes = "Le format doit être 0000-AA-00"
Else
    If Len(x) > 2 Then x = Application.Replace(x, 3, 0, "-")
    If Len(x) > 6 Then x = Application.Replace(x, 7, 0, "-")
    x = UCase(Left(x, 9))
    If Len(x) = 9 Then If Not x Like "[A-Z][A-Z]-###-[A-Z][A-Z]" Then mes = "Le format doit être AA-000-AA"
End If
TextBox1 = x
If mes <> "" Then MsgBox mes
End Sub
Je vais abuser :
Est-il possible de prévoir une immatriculation avec 2, 3 ou 4 chiffres en premier tronçon (anicenne immatriculation) ?
1-AS-80
12-AS-80
123-AS-80
Merci, cordialement, Gérard
 

Discussions similaires

Réponses
4
Affichages
244
Réponses
13
Affichages
483
Réponses
2
Affichages
227

Statistiques des forums

Discussions
314 190
Messages
2 107 002
Membres
109 735
dernier inscrit
Mounskad