Vba excel - plusieurs textbox

Résolu/Fermé
eideal Messages postés 21 Date d'inscription mercredi 23 juin 2010 Statut Membre Dernière intervention 20 février 2017 - 30 juin 2011 à 22:54
eideal Messages postés 21 Date d'inscription mercredi 23 juin 2010 Statut Membre Dernière intervention 20 février 2017 - 4 juil. 2011 à 22:44
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
A voir également:

1 réponse

michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
1 juil. 2011 à 09:48
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...
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 745
1 juil. 2011 à 10:13
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
0
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
1 juil. 2011 à 10:27
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
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 745
1 juil. 2011 à 10:33
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é...
0
michel_m Messages postés 16603 Date d'inscription lundi 12 septembre 2005 Statut Contributeur Dernière intervention 16 décembre 2023 3 303
1 juil. 2011 à 11:26
Bin, il est 11,25h

Garçon, Avec 2 glaçons, siouplait
0
pijaku Messages postés 12263 Date d'inscription jeudi 15 mai 2008 Statut Modérateur Dernière intervention 4 janvier 2024 2 745
Modifié par pijaku le 1/07/2011 à 11:36
Excellent!!
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é!!
0