1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| Sub JourOuvré()
NbJoursOuvrés = 0
HeureRefDeb = TimeValue("9:00")
HeureRefFin = TimeValue("18:00")
FlagNonOuvré = 0
'Parcour mon tab Excel
For p = 2 To Cells(65535, 1).End(xlUp).Row
'Stock des dates et heures dans variables
Dt_Clos = Format(Range("D" & p).Value, "dd/mm/yy")
Dt_Ouvert = Format(Range("C" & p).Value, "dd/mm/yy")
H_Clos = Format(Range("D" & p).Value, "hh:mm")
H_Ouvert = Format(Range("C" & p).Value, "hh:mm")
Heures = 1 / 24 '0.041666666667 (N° de série)
DateDemande = DateValue(Dt_Ouvert)
DateTraitement = DateValue(Dt_Clos)
HeureDemande = TimeValue(H_Clos)
HeureTraitement = TimeValue(H_Ouvert)
'Calcul horaire entre heure de la demande et heure du traitement
TpsHeureTraitement = (HeureRefFin - HeureTraitement) + (HeureDemande - HeureRefDeb)
If DateDemande = DateTraitement Then TpsHeureTraitement = TpsHeureTraitement - (9 * Heures)
If TpsHeureTraitement >= (18 * Heures) Then
TpsHeureTraitement = TpsHeureTraitement - (9 * Heures)
End If
'on considère que le 1er et dernier jour sont des jours ouvrés
For i = DateDemande + 1 To DateTraitement - 1
If CStr(Application.WorksheetFunction.Weekday(i)) Like ("[2-6]") Then
Call VerifFerié(i, Férié)
'si on a un jour férié, on le retranche
NbJoursOuvrés = NbJoursOuvrés + 1 + Férié 'Si férié = true, férié = -1
End If
Next
'on vérifie si 1er et dernier jour sont des jours ouvrés
If Not CStr(Application.WorksheetFunction.Weekday(DateDemande)) Like ("[2-6]") Then FlagNonOuvré = 1
If Not CStr(Application.WorksheetFunction.Weekday(DateTraitement)) Like ("[2-6]") Then FlagNonOuvré = 1
Call VerifFerié(DateDemande, Férié)
If Férié = True Then FlagNonOuvré = 1
Call VerifFerié(DateTraitement, Férié)
If Férié = True Then FlagNonOuvré = 1
NbJoursOuvrés = NbJoursOuvrés + Int((24 * TpsHeureTraitement) / 9)
If TpsHeureTraitement >= (9 * Heures) Then
TpsHeureTraitement = TpsHeureTraitement - (9 * Heures * (NbreJrs + 1))
End If
délaiH = Format(TpsHeureTraitement, "hh:mm")
'MsgBox "Délai d'intervention" & vbCr & "Nbre de jours ouvrés " & NbJoursOuvrés & vbCr & "Nombre d'heures " & délaiH
'Cellule_Result = NbJoursOuvrés & " jours " & délaiH
Cellule_Result = CStr((NbJoursOuvrés * 9 + Hour(délaiH))) & ":" & CStr(Minute(délaiH))
If FlagNonOuvré = 0 Then
Range("E" & p) = Cellule_Result
Range("E" & p).NumberFormat = "[h]:mm"
Else
Range("E" & p) = "Date début ou fin : jour non ouvré !"
End If
NbJoursOuvrés = 0
délaiH = 0
FlagNonOuvré = 0
Next p
End Sub
Sub VerifFerié(DateAtraiter, Férié)
JourFérié = Array("01/01/2008", "24/03/2008") ' etc
For n = 0 To UBound(JourFérié)
Férié = DateAtraiter = DateValue(JourFérié(n)) ''si vrai, Férié = -1
If Férié Then Exit For
Next
End Sub |
Partager