Vba excel - plusieurs textbox
Résolu
eideal
Messages postés
20
Date d'inscription
Statut
Membre
Dernière intervention
-
eideal Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
eideal Messages postés 20 Date d'inscription Statut Membre Dernière intervention -
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
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
A voir également:
- Vba excel - plusieurs textbox
- Liste déroulante excel - Guide
- Formule moyenne excel plusieurs colonnes - Guide
- Word et excel gratuit - Guide
- Classer par ordre alphabétique excel plusieurs colonnes - Guide
- Formule excel pour additionner plusieurs cellules - Guide
1 réponse
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...
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...
Pour le "truc" de la virgule je pense qu'il souhaite quelque chose comme ceci :
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
S'il avait été 11h30 j't'aurais bien proposé un p'tit past*s, mais vu l'heure, j'vais prendre un café...
Garçon, Avec 2 glaçons, siouplait
voici donc pour commencer
Sans oublier...
Juste au cas ou l'apéro durerait en longueur...
il est bien 11h30???
Ps : qu'est ce qu'il peux y avoir comme conn*rie de stocké sur cjoint...
EDIT : non 11h36, 6 minutes de retard. Désolé!!