Vba excel - plusieurs textbox

Résolu
Bonjour,

Bonjour, J'ai plusieurs textbox dans un userform 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

1 réponse

  1. Contributeur
    Bonjour

    ci joint un 1° allègement avec 1 macro textbox au lieu de 4 en utilisant un module de classe
    https://www.cjoint.com/?3GbjVRhkUvw
    j'avoue ne pas avoir trop compris ton histoire de virgule...
    la macro textbox se trouve maintenant dans le module de classe...
    1. Modérateur
      Salut michel, eideal,
      Pour le "truc" de la virgule je pense qu'il souhaite quelque chose comme ceci :
      Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
      If TextBox1 = "" Then Exit Sub
      If InStr(TextBox1, ",") = 0 And InStr(TextBox1, ".") = 0 Then
          TextBox1 = TextBox1 & ",00"
      End If
      End Sub
    2. Contributeur
      Salut Pijaku,

      tu as certainement raison, j'y avais vaguement pensé mais avec un lostfocus (pas sûr de moi).
      On va attendre la réaction de Fred si tu veux bien
    3. Modérateur
      Tout à fait! Wait and see...
      S'il avait été 11h30 j't'aurais bien proposé un p'tit past*s, mais vu l'heure, j'vais prendre un café...
    4. Contributeur
      Bin, il est 11,25h

      Garçon, Avec 2 glaçons, siouplait