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
| Option Explicit
Public WithEvents MonTextbox As MSForms.TextBox
Private Sub MonTextbox_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
'Transforme le "." en ","
If KeyAscii = 46 Then KeyAscii = 44
'Refuse les caractères autres que numériques (ou virgule)
If KeyAscii <> 44 And (KeyAscii < 48 Or KeyAscii > 57) Then
KeyAscii = 0
MonTextbox.BackColor = &HFF&
Call AfficherAlerte("Caractère interdit")
Exit Sub
End If
'Refuse la saisie d'une deuxième virgule
If KeyAscii = 44 And InStr(MonTextbox, ",") > 0 Then
KeyAscii = 0
MonTextbox.BackColor = &HFF&
Call AfficherAlerte("Une seule virgule possible")
Exit Sub
End If
'Refuse une virgule si le textbox est vide
If KeyAscii = 44 And Len(MonTextbox) = 0 Then
KeyAscii = 0
MonTextbox.BackColor = &HFF&
Call AfficherAlerte("Pas de virgule si le textbox est vide")
Exit Sub
End If
'Empêche de saisir plus de 10 chiffres avant la virgule
If KeyAscii <> 44 Then
If MonTextbox <> "" Then
If Len(Int(MonTextbox)) > 9 Then
If MonTextbox.SelStart < InStr(MonTextbox, ",") Or InStr(MonTextbox, ",") = 0 Then
KeyAscii = 0
MonTextbox.BackColor = &HFF&
Call AfficherAlerte("Pas plus de 10 chiffres avant la virgule")
Exit Sub
End If
End If
End If
End If
'Empêche de saisir plus de 2 chiffres après la virgule
If Left(Right(MonTextbox.Value, 3), 1) = "," And MonTextbox.SelStart > Len(MonTextbox.Value) - 3 Then
KeyAscii = 0
MonTextbox.BackColor = &HFF&
Call AfficherAlerte("Pas plus de 2 chiffres après la virgule")
Exit Sub
End If
End Sub
Private Sub MonTextbox_Change()
MonTextbox.BackColor = &H80000005
Call ResetAlerte
End Sub |
Partager