plusieurs textbox en valeur numerique : module de classe (résolu)

eideal44

XLDnaute Junior
Bonjour, J'ai plusieurs textbox dans un userform sur excel 2000 et j'ai indiqué pour chaque textbox un code vba afin de n'accepter que la virgule et pouvoir la compléter 2 chiffres après la virgule mais j'aimerais savoir si quelqu'un pourrait m'aider pour réduire le code car il est très long:

'TEXTBOX EN VALEUR NUMERIQUE + 2 CHIFFRES APRES LA VIRGULE
Private Sub Textbox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890,-", Chr(KeyAscii)) = 0 Or Textbox1.SelStart > 0 And Chr(KeyAscii) = "-" _
Or InStr(Textbox1.Value, ",") <> 0 And Chr(KeyAscii) = "," Then
KeyAscii = 0: Beep
End If
If Right(Textbox1, 1) = "." Or Right(Textbox1, 1) = "," Then
Textbox1.MaxLength = Len(Textbox1) + 2
End If
End Sub

Private Sub Textbox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890,-", Chr(KeyAscii)) = 0 Or Textbox2.SelStart > 0 And Chr(KeyAscii) = "-" _
Or InStr(Textbox2.Value, ",") <> 0 And Chr(KeyAscii) = "," Then
KeyAscii = 0: Beep
End If
If Right(Textbox2, 1) = "." Or Right(Textbox2, 1) = "," Then
Textbox2.MaxLength = Len(Textbox2) + 2
End If
End Sub

Private Sub Textbox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890,-", Chr(KeyAscii)) = 0 Or Textbox3.SelStart > 0 And Chr(KeyAscii) = "-" _
Or InStr(Textbox3.Value, ",") <> 0 And Chr(KeyAscii) = "," Then
KeyAscii = 0: Beep
End If
If Right(Textbox3, 1) = "." Or Right(Textbox3, 1) = "," Then
Textbox3.MaxLength = Len(Textbox3) + 2
End If
End Sub

Private Sub Textbox4_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890,-", Chr(KeyAscii)) = 0 Or Textbox4.SelStart > 0 And Chr(KeyAscii) = "-" _
Or InStr(Textbox4.Value, ",") <> 0 And Chr(KeyAscii) = "," Then
KeyAscii = 0: Beep
End If
If Right(Textbox4, 1) = "." Or Right(Textbox4, 1) = "," Then
Textbox4.MaxLength = Len(Textbox4) + 2
End If
End Sub

-------------------------

de plus j'aimerais savoir si il existe un code pour que quand on quitte un textbox celui se mette automatiquement 2 chiffres après la virgule (ex: notez 99 dans un textbox et dès qu'on passe au 2ième, ça inscrit 99,00).

dans l'attente de vos lumières car je galère un peu

FRED
 
Dernière édition:

Discussions similaires

Réponses
6
Affichages
247

Statistiques des forums

Discussions
312 228
Messages
2 086 421
Membres
103 205
dernier inscrit
zch