[VBA]Textbox de Userform Contrôlé

Résolu/Fermé
lml-mike Messages postés 453 Date d'inscription vendredi 16 février 2007 Statut Contributeur Dernière intervention 18 novembre 2018 - 7 sept. 2010 à 11:48
lml-mike Messages postés 453 Date d'inscription vendredi 16 février 2007 Statut Contributeur Dernière intervention 18 novembre 2018 - 7 sept. 2010 à 12:09
Bonjour,

Cela se passe sur 3 textbox, une contient une valeur affichée (non saisie), les deux autres sont saisissables.

Je cherche à effectuer un équilibrage de ces deux autres textbox par rapport à la première.

En gros, si 10 est affichée sur la première :

- si je saisi 5 sur la deuxième, la troisième affiche 5
- si je saisi 7 sur la deuxième, la troisième affiche 3
- si je saisi 6 sur la troisième, la deuxième affiche 4
- si je saisi 2 sur la troisième, la deuxième affiche 8
- si je saisi 10 sur la deuxième, la troisième affiche 0
etc...

J'ai déjà un code de contrôle de saisie, mais il me manque pour commencer la partie ou le chiffre rentré ne peut PAS être supérieur à la valeur de la première textbox.

Private Sub UserForm_Initialize()

Text3.value = text1.value
Text2.value = 0

End Sub

Public Sub Text2_change()

Dim Prix As Single
Dim prixtot As Single

If Text2.Value = "" Then
Exit Sub
Else
Prix = Text2.Value
Text3.value = Text1.Value - Prix
End If

end sub

Private Sub Text2_Enter()

Text2.Value = ""

End Sub

Public Sub Text2_keypress(ByVal Keyascii As MSForms.ReturnInteger)
   
    If InStr("1234567890.", Chr(Keyascii)) = 0 _
    Or InStr(Text1.Value, ".") <> 0 And Chr(Keyascii) = "." Then
       Keyascii = 0: Beep
    End If

End Sub

'___________________________

Public Sub Text3_change()

Dim Prix As Single
Dim prixtot As Single

If Text3.Value = "" Then
Exit Sub
Else
Prix = Text3.Value
Text2.value = Text1.Value - Prix
End If

end sub

Private Sub Text3_Enter()

Text3.Value = ""

End Sub

Public Sub Text3_keypress(ByVal Keyascii As MSForms.ReturnInteger)
   
    If InStr("1234567890.", Chr(Keyascii)) = 0 _
    Or InStr(Text1.Value, ".") <> 0 And Chr(Keyascii) = "." Then
       Keyascii = 0: Beep
    End If

End Sub



Merci pour votre aide !

1 réponse

lml-mike Messages postés 453 Date d'inscription vendredi 16 février 2007 Statut Contributeur Dernière intervention 18 novembre 2018 120
7 sept. 2010 à 12:09
Bon ben problème réglé lol !

Il faut simplement mettre une condition si la valeur est négative pour mettre la valeur maximale à la place sur la condition change ^^'

Private Sub UserForm_Initialize()

text3.Value = text1.Value
text2.Value = 0

End Sub

Public Sub Text2_change()

Dim Prix As Single
Dim prixtot As Single

If text2.Value = "" Then
    Exit Sub
Else
    text3.Value = text1.Value - text2.Value
    If text3.Value < 0 Then
        text3.Value = 0
        text2.Value = text1.Value
    End If
End If

End Sub

Private Sub Text2_Enter()

text2.Value = ""

End Sub

Public Sub Text2_keypress(ByVal Keyascii As MSForms.ReturnInteger)
   
    If InStr("1234567890.", Chr(Keyascii)) = 0 _
    Or InStr(text1.Value, ".") <> 0 And Chr(Keyascii) = "." Then
       Keyascii = 0: Beep
    End If

End Sub
Public Sub Text3_change()

Dim Prix As Single
Dim prixtot As Single

If text3.Value = "" Then
    Exit Sub
Else
    text2.Value = text1.Value - text3.Value
    If text2.Value < 0 Then
        text2.Value = 0
        text3.Value = text1.Value
    End If
End If

End Sub

Private Sub Text3_Enter()

text3.Value = ""

End Sub

Public Sub Text3_keypress(ByVal Keyascii As MSForms.ReturnInteger)
   
    If InStr("1234567890.", Chr(Keyascii)) = 0 _
    Or InStr(text1.Value, ".") <> 0 And Chr(Keyascii) = "." Then
       Keyascii = 0: Beep
    End If

End Sub

1