1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
Private Sub worksheet_change(ByVal target As Range)
'permet de sortir de la procédure si plus d'une cellule est sélectionnée
'sinon la suite de la macro renvoie un message d'erreur
If target.Count > 1 Then Exit Sub
If Intersect(target, Range("a2:a200")) Is Nothing Then
Exit Sub
Else
'vérification que la cellule est remplie
If Len(target.Value) = 0 Then
Exit Sub
Else
'Vérification que ce n'est que des chiffres
If Not target.Value Like String(Len(target.Value), "#") Then
target.Interior.ColorIndex = 3
target.NumberFormat = "General"
target.Select
MsgBox "Votre code ne doit contenir que des chiffres"
Exit Sub
End If
'Vérification que le premier caractère est un zéro
'Cette partie du code ne fonctionne pas :-(
If Left(target.Value, 1) = "0" Then
If Len(target.Value) = 4 Then
target.Interior.ColorIndex = 2
target.NumberFormat = "0# ###"
Else
If Len(target.Value) = 5 Then
target.Interior.ColorIndex = 2
target.NumberFormat = "0# ## ##"
End If
End If
Else
'Vérifie qu'il y a au moins 5 caractères
If Len(target.Value) < 5 Then
target.Interior.ColorIndex = 3
target.NumberFormat = "General"
target.Select
MsgBox "Votre code est inférieur à 5 caractères. Merci de vérifier votre saisie"
End If
'Vérifie qu'il n'y a pas plus de 6 caractères
If Len(target.Value) > 6 Then
target.Interior.ColorIndex = 3
target.NumberFormat = "General"
target.Select
MsgBox "Votre code est supérieur à 6 caractères. Merci de vérifier votre saisie"
End If
'Vérifie qu'il y a 6 caractères
If Len(target.Value) = 6 Then
target.Interior.ColorIndex = 2
target.NumberFormat = "## ## ##"
End If
'Vérifie qu'il y a 5 caractères
If Len(target.Value) = 5 Then
target.Interior.ColorIndex = 2
target.NumberFormat = "## ###"
End If
End If
End If
End If
End Sub |
Partager