Microsoft 365 Addition entre combobox

  • Initiateur de la discussion Initiateur de la discussion Marvin57
  • Date de début Date de début

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

Marvin57

XLDnaute Occasionnel
Bonjour à tout le monde,

Est-ce que quelqu'un pourrait me dire comment additionner deux combobox (combobox1 et combobox2) qui sont au format heure et mettre le redultat dans le textbox5.

C'est à dire, si le combobox1 affiche 07:00:00 et le combobox2 affiche 12:00:00 alors j'aimerai qu'il affiche en textbox5 le chiffre 5. Ou si le résultat donne par exemple un résultat de 01h30 alors il devrait afficher 1.5

Merci d'avance à vous.

Marvin57
 
Solution
Il se passe un étrange phénomène qui boucle sur le CDate et finit par planter brutalement Excel.
Le .Text aussi est problématique.
VB:
Private Sub ComboBox1_Change()
    Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox2.Value = Format(Me.ComboBox2.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Value) > CDate(Me.ComboBox1.Value) Then
        Me.TextBox5.Value = Round(24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    Else
        Me.TextBox5.Value = Round(24 + 24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    End If
    On Error GoTo 0
End Sub
Bonjour,

Tu mets dans ces 2 évènements:
- Private Sub ComboBox1_Change()
- Private Sub ComboBox2_Change()

VB:
Private Sub ComboBox1_Change()
    On Error Resume Next
    Me.ComboBox1.Text = CDate(Me.ComboBox1.Text)
    Me.TextBox5.Text = 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
End Sub

Private Sub ComboBox2_Change()
    On Error Resume Next
    Me.ComboBox2.Text = CDate(Me.ComboBox2.Text)
    Me.TextBox5.Text = 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
End Sub
 
Dernière édition:
Ou plus exactement, s'il peut y avoir recouvrement sur 24 heures:
VB:
Private Sub ComboBox1_Change()
    On Error Resume Next
    Me.ComboBox1.Text = CDate(Me.ComboBox1.Text)
    On Error GoTo 0
    
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    On Error Resume Next
    Me.ComboBox2.Text = CDate(Me.ComboBox2.Text)
    On Error GoTo 0
    
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Text) > CDate(Me.ComboBox1.Text) Then
        Me.TextBox5.Text = 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
    Else
        Me.TextBox5.Text = 24 + 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
    End If
    On Error GoTo 0
End Sub
 
Ou plus exactement, s'il peut y avoir recouvrement sur 24 heures:
VB:
Private Sub ComboBox1_Change()
    On Error Resume Next
    Me.ComboBox1.Text = CDate(Me.ComboBox1.Text)
    On Error GoTo 0
   
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    On Error Resume Next
    Me.ComboBox2.Text = CDate(Me.ComboBox2.Text)
    On Error GoTo 0
   
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Text) > CDate(Me.ComboBox1.Text) Then
        Me.TextBox5.Text = 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
    Else
        Me.TextBox5.Text = 24 + 24 * (CDate(Me.ComboBox2.Text) - CDate(Me.ComboBox1.Text))
    End If
    On Error GoTo 0
End Sub
Bonsoir Dudu2,

Merci pour votre intervention et aide. Mais j'ai un petit soucis.
J'aurai du vous mettre en premier temps les codes existants.

Alors voici comment étaient mes codes des deux combobox

VB:
Private Sub ComboBox2_Change()
ComboBox2 = Format(ComboBox2, "hh:mm")
End Sub

Private Sub Combobox1_Change()
ComboBox1 = Format(ComboBox1, "hh:mm")
End Sub

donc si je rajoute vos codes cela donne un bug !!

Auriez-vous une idée pour cela SVP ?

Marvin57
 
Il se passe un étrange phénomène qui boucle sur le CDate et finit par planter brutalement Excel.
Le .Text aussi est problématique.
VB:
Private Sub ComboBox1_Change()
    Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox2.Value = Format(Me.ComboBox2.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Value) > CDate(Me.ComboBox1.Value) Then
        Me.TextBox5.Value = Round(24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    Else
        Me.TextBox5.Value = Round(24 + 24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    End If
    On Error GoTo 0
End Sub
 
Un bug, c'est plutôt vague.
Soit tu places ici ton fichier, soit tu donnes un screenshot du message d'erreur et de l'instruction qui plante en choisissant <Debug> sur le message d'erreur.
Ré,
Désolé Dudu2, je m'étais trompé. Cela fonctionne très bien. 👍👍
J'avais pris un ancien fichier de hier, donc cela ne fonctionnait pas. 🤨

Merci beaucoup pour votre aide.
Peut-être à une prochaine fois.
Marvin57
 
Il se passe un étrange phénomène qui boucle sur le CDate et finit par planter brutalement Excel.
Le .Text aussi est problématique.
VB:
Private Sub ComboBox1_Change()
    Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox2.Value = Format(Me.ComboBox2.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Value) > CDate(Me.ComboBox1.Value) Then
        Me.TextBox5.Value = Round(24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    Else
        Me.TextBox5.Value = Round(24 + 24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    End If
    On Error GoTo 0
End Sub
Re
Erreur chez moi également !!!!

J'étais trop rapide. Une seule fois il fonctionnait et voilà que cela bug à nouveau.
 
Il se passe un étrange phénomène qui boucle sur le CDate et finit par planter brutalement Excel.
Le .Text aussi est problématique.
VB:
Private Sub ComboBox1_Change()
    Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox2.Value = Format(Me.ComboBox2.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Value) > CDate(Me.ComboBox1.Value) Then
        Me.TextBox5.Value = Round(24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    Else
        Me.TextBox5.Value = Round(24 + 24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    End If
    On Error GoTo 0
End Sub
Je ferai des essais dans la matinée et je reviendrai.

En attendant Merci à vous.
 
Il se passe un étrange phénomène qui boucle sur le CDate et finit par planter brutalement Excel.
Le .Text aussi est problématique.
VB:
Private Sub ComboBox1_Change()
    Me.ComboBox1.Value = Format(Me.ComboBox1.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub ComboBox2_Change()
    Me.ComboBox2.Value = Format(Me.ComboBox2.Value, "hh:mm")
    Call SetTextBox5
End Sub

Private Sub SetTextBox5()
    On Error Resume Next
    If CDate(Me.ComboBox2.Value) > CDate(Me.ComboBox1.Value) Then
        Me.TextBox5.Value = Round(24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    Else
        Me.TextBox5.Value = Round(24 + 24 * (CDate(Me.ComboBox2.Value) - CDate(Me.ComboBox1.Value)), 0)
    End If
    On Error GoTo 0
End Sub
Bonjour Dudu2,

voila, tout fonctionne maintenant.

Vraiment un Grand Merci à vous pour votre aide.

A très vite.
Marvin57
 
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
40
Affichages
215
Réponses
22
Affichages
936
Réponses
8
Affichages
670
  • Question Question
Microsoft 365 Erreur de format
Réponses
5
Affichages
457
Réponses
13
Affichages
598
Retour