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
| Option Explicit
Sub CalculeDureeTotalSite()
Const Cible As String = "K2"
Dim Str As String, LaCause As String, Duree As String
Dim M1 As Byte, M2 As Byte, MoisNum As Byte
Dim LastLig As Long, i As Long, n As Long
Dim MonDico As New Scripting.Dictionary
Dim Temps As Double
Dim Tb, Res()
Application.ScreenUpdating = False
With Feuil2
LastLig = .Cells(.Rows.Count, "A").End(xlUp).Row
.Range(Cible).Resize(LastLig, 4).ClearContents
Tb = .Range("A2:F" & LastLig)
LaCause = .[Cause]
MoisNum = Application.Match(.[Mois], .
[ListeMois], 0)
For i = 1 To UBound(Tb, 1)
If Tb(i, 5) = LaCause Then
M1 = Month(Tb(i, 2))
M2 = Month(Tb(i, 3))
If Entre(MoisNum, M1, M2) Then
If M1 < MoisNum Then Tb(i, 2) = DebMois(Tb(i, 2), MoisNum)
If M2 > MoisNum Then Tb(i, 3) = FinMois(Tb(i, 3), MoisNum)
Duree = CStr(CDbl(Tb(i, 3)) - CDbl(Tb(i, 2)))
Str = Tb(i, 1)
If Not MonDico.Exists(Str) Then
MonDico.Add Str, Duree
Else
MonDico(Str) = MonDico(Str) & "+" & Duree
End If
End If
End If
Next i
n = MonDico.Count
If n > 0 Then
ReDim Res(1 To n, 1 To 4)
For i = 0 To n - 1
Debug.Print i, MonDico.Keys(i), MonDico.Items(i)
Res(i + 1, 1) = MonDico.Keys(i)
Tb = Split(MonDico.Items(i), "+")
Res(i + 1, 2) = UBound(Tb) + 1
Erase Tb
Temps = Evaluate(MonDico.Items(i))
Res(i + 1, 3) = Temps
Res(i + 1, 4) = Round(1440 * Temps)
Next i
Set MonDico = Nothing
.Range(Cible).Resize(n, 4) = Res
.Range(Cible).Offset(0, 2).Resize(n, 1).NumberFormat = "dd ""jour(s)"" hh"" heure(s) ""mm"" minute(s)"""
.Range(Cible).Resize(n, 4).Sort Key1:=.Range(Cible), Order1:=xlAscending, Header:=xlNo
End If
End With
End Sub
Private Function Entre(ByVal M As Byte, ByVal Mi As Byte, ByVal Mf As Byte) As Boolean
Entre = M >= Mi And M <= Mf
End Function
Private Function DebMois(ByVal Dte As Long, ByVal M As Byte) As Long
DebMois = DateSerial(Year(Dte), M, 1)
End Function
Private Function FinMois(ByVal Dte As Long, ByVal M As Byte) As Double
FinMois = DateSerial(Year(Dte), M + 1, 1) - 1 / 1440
End Function |
Partager