Calcul durée de nuit par userform
Bonjour à tous,
j'ai appris, par ce forum, à calculer une durée comprise entre deux horaires à l'aide d'un petit userform.
Quand les horaires concernent le même jour, il n'y a pas de soucis : tout fonctionne.
Or, quand les horaires sont à cheval entre deux jours (horaires de nuit), comment faire pour que l'userform calcul correctement la différence ?
Petit exemple :
textbox 1 : début du travail : 22:00
textbox2 : fin du travail : 01:00
textbox3 : total : 03:00
Or, la formule suivante que j'ai essayé de créer ne fonctionne pas :
Dim H1 As Date
Dim H2 As Date
H1 = CDate(TextBox1.Value)
H2 = CDate(TextBox2.Value)
If H2 < H1 Then
TextBox2.Value = Format(("24:00" - H1) + (H2 - "00:00"), "hh:mm")
Else
TextBox3.Value = Format(H2 - H1, "hh:mm")
End If
je vous fais parvenir en pièce jointe un petit fichier simplifié.
Merci pour votre aide à venir,
seb
bonjour,
remplacer tout le code comme suit :
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub TextBox1_Change()
If Len(TextBox1) = 5 Then TextBox2.SetFocus
End Sub
Private Sub TextBox1_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim touche_autorisée As String
touche_autorisée = "[01234567989:]"
'on ne peut pas taper autre chose que des chiffres
If Not ChrW(KeyAscii) Like touche_autorisée Then KeyAscii = 0
'ici on empeche de taper plus de 4 chiffre(2 pour l'heure,2pour les minutes )
If Len(TextBox1) = 5 Then KeyAscii = 0: Exit Sub
'ici on empeche de taper plus gros que 2 pour le premier chiffre de l'heure
If Len(TextBox1) = 0 And Not ChrW(KeyAscii) Like "[0-1-2]" Then KeyAscii = 0
'ici on empeche de taper plus gros que 3 pour le deuxieme chiffre de l'heure en fonction du premier
If Len(TextBox1) = 1 And TextBox1.Value = 2 And Not ChrW(KeyAscii) Like "[1-2-3]" Then KeyAscii = 0
'ici on empeche de taper plus gros que 5 pour les dizaine de minute(59 minutes maximum)
If Len(TextBox1) = 3 And Not ChrW(KeyAscii) Like "[0-1-2-3-4-5]" Then KeyAscii = 0
If Len(TextBox1) = 2 Then
If ChrW(KeyAscii) <> ":" Then KeyAscii = Asc(":")
End If
End Sub
Private Sub TextBox2_Change()
If Len(TextBox2) = 5 Then TextBox3.SetFocus
End Sub
Private Sub TextBox2_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Dim touche_autorisée As String
touche_autorisée = "[01234567989:]"
If Not ChrW(KeyAscii) Like touche_autorisée Then KeyAscii = 0
If Len(TextBox12) = 5 Then KeyAscii = 0: Exit Sub
If Len(TextBox2) = 0 And Not ChrW(KeyAscii) Like "[0-1-2]" Then KeyAscii = 0
If Len(TextBox2) = 1 And TextBox2.Value = 2 And Not ChrW(KeyAscii) Like "[1-2-3]" Then KeyAscii = 0
If Len(TextBox2) = 3 And Not ChrW(KeyAscii) Like "[0-1-2-3-4-5]" Then KeyAscii = 0
If Len(TextBox1) = 2 Then
If ChrW(KeyAscii) <> ":" Then KeyAscii = Asc(":")
End If
End Sub
Private Sub TextBox2_afterupdate()
Dim H1 As Date
Dim H2 As Date
H1 = CDate(TextBox1.Value)
H2 = CDate(TextBox2.Value)
If H2 < H1 Then H2 = H2 + 24
TextBox3.Value = Format(H2 - H1, "hh:mm")
End SubA+
merci vivement pour la rapidité de ta réponse. Je m'atèle à décortiquer le code pour bien le comprendre.
Bonne soirée
Seb