Problème de soustraction d'heure et minute

Résolu
2015-1158 Messages postés 34 Statut Membre -  
 archer -
Bonjour,

Je souhaite coder un UserForm me permettant avec des comboBox de choisir des plages horaires (Borne Inf et Born Sup)
Les comboBox sont de la forme suivante liste déroulante=00:00 , 00:30, 01:00, 01:30 , 02:00...
Ensuite après avoir choisit la borne Inf et la Borne Sup je souhaite faire la soustraction et l'afficher dans un TextBox1

Je récapitule, l'UserForm s'ouvre.
Il y a deux ComboBox1 et ComboBox2 avec des choix de temps. près le choix la soustraction doit s'effectuer dans un TextBox1.

Voici ce que j'ai actuellement codé:

#Private Sub UserForm_Initialize()
    Dim i As Byte, j As Byte
    'je boucle sur les heures
    For i = 0 To 23
    'je boucle sur les minutes par tranche de 30mn
        For j = 0 To 59 Step 30
        'j'initialize ma combo
        Me.ComboBox1.AddItem Format(i, "00") & ":" & Format(j, "00")
        Me.ComboBox2.AddItem Format(i, "00") & ":" & Format(j, "00")
        Next j
    Next i
End Sub

Sub ComboBox1_Change()
    CalculPour
End Sub

Sub ComboBox2_Change()
    CalculPour
End Sub

Private Sub CalculPour()
    Dim hh As Integer
    Dim mn As Integer
    Dim ss As Integer
    Dim t1 As Date
    Dim t2 As Date
    hh = 0
    mn = 0
    ss = 0
    t1 = ComboBox1.Value
    t2 = ComboBox2.Value
    ss = DateDiff(DateInterval.Second, t1, t2)
    TextBox1.Text = Format(hh, "00") & "h" & Format(mn, "00") & "'" & Format(ss, "00")
End Sub
#

Le problème est du type 13 sur les t1=ComboBox1.Value, il n'arrive pas à lire.

merci pour votre aide.

2 réponses

  1. f894009 Messages postés 17417 Date d'inscription   Statut Membre Dernière intervention   1 717
     
    Bonjour,

    Private Sub CalculPour()
        Dim hh As Integer
        Dim Mn As Integer
        Dim ss As Integer
        Dim t1 As Date
        Dim t2 As Date
        hh = 0      '??????????????????
        Mn = 0         '?????????????????????
        ss = 0      '?????????????????????????
        If ComboBox1 <> "" And ComboBox2 <> "" Then
            t1 = ComboBox1.Value
            t2 = ComboBox2.Value
            TextBox1.Text = CalculDifferenceHeure(t1, t2)
        End If
    End Sub
    
    'https://www.generation-nt.com/reponses/calculer-difference-entre-2-heures-entraide-2422711.html
    Function CalculDifferenceHeure(HD As Variant, HF As Variant) As String
        'HD Heure de début
        'HF Heure de fin
        Dim Secondes As Long
        Dim Sec As String
        Dim Minutes As Long
        Dim Mn As String
        Dim Heures As Long
        Dim Hr As String
     
        Secondes = DateDiff("s", HD, HF)        '* -1 a ajouter si t1 est toujours plus grand que t2 sinon temps negatif ....!!!!
    
        Heures = Secondes / 3600
        Minutes = Secondes / 60
    
        Mn = Minutes - (Heures * 60)
        Hr = Heures
    
        If Hr < 10 Then Hr = "0" & Heures
        If Mn < 10 Then Mn = "0" & Mn
        Sec = Secondes - (Minutes * 60)
        If Sec < 10 Then Sec = "0" & Sec
        CalculDifferenceHeure = Hr & ":" & Mn & ":" & Sec
     End Function
    0
  2. archer
     
    Bonjour
    A voir aussi comme ca
    A+
    Maurice
    Dim Ctrl As Control
    
    Private Sub UserForm_Initialize()
    Me.Caption = "TACHES ??????"
    'Me.BackColor = &H80000005
    Me.PictureSizeMode = 1
    He = 24
       For Each Ctrl In Me.Controls
          Select Case Left(Ctrl.Name, 3)
              Case "Tex"
                Ctrl.BackColor = &H80000018
                Ctrl.Font.Name = "Arial Narrow"
                Ctrl.Font.Size = 14
                Ctrl.Font.Bold = True
                Ctrl.Height = He
                Ctrl.Tag = "O"
              Case "Com"
                Ctrl.BackColor = &H80000005
                Ctrl.BackStyle = 0
                Ctrl.Font.Name = "Arial Narrow"
                Ctrl.Font.Size = 14
                Ctrl.Font.Bold = True
           End Select
       Next Ctrl
    End Sub
    
    Private Sub UserForm_Activate()
    Dim I&, J&
    'je boucle sur les heures
        For I = 0 To 23
        'je boucle sur les minutes par tranche de 30mn
            For J = 0 To 59 Step 30
            'j'initialize ma combo
            ComboBox1.AddItem Format(I, "00") & ":" & Format(J, "00")
            Next
        Next
    End Sub
    
    Sub ComboBox1_Change()
    Dim I&
    ComboBox2.Clear
        For I = 0 To ComboBox1.ListCount - 1
            If ComboBox1.Value < ComboBox1.List(I) Then
                ComboBox2.AddItem ComboBox1.List(I)
            End If
        Next I
    End Sub
    
    Sub ComboBox2_Change()
    Dim H1 As Date
    Dim H2 As Date
    H1 = CDate(ComboBox1.Value)
    H2 = CDate(ComboBox2.Value)
    'TextBox1.Value = Format(H1 + H2, "hh:mm")
    TextBox1.Value = Format(H2 - H1, "hh:mm")
    End Sub
    
    0