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 73
| Sub Test1()
Dim SrcRng As Range
Dim SumVal As Double, CntVal As Integer
Dim RowN As Integer, ColN As Integer
Dim MinDate As Date, MaxDate As Date, DDate As Date, DYear As Integer
Dim DatRefAR() As Variant
Dim RefMonth As Integer, YearPart As Integer, YearCnt As Integer
' Init
Set SrcRng = ThisWorkbook.Worksheets("Données").Range("A3").CurrentRegion
RefMonth = 4 'à changer si besoin
' Extrait les dates min et max des données
MinDate = WorksheetFunction.Min(SrcRng.Columns(1))
MaxDate = WorksheetFunction.Max(SrcRng.Columns(1))
' Crée un tableau contenant toutes les dates de référence
YearCnt = 0
ReDim DatRefAR(1 To (Year(MaxDate) - Year(MinDate)) + 1, 1 To 3)
For YearPart = Year(MinDate) To Year(MaxDate)
YearCnt = YearCnt + 1
DatRefAR(YearCnt, 1) = YearPart 'Année
DatRefAR(YearCnt, 2) = DateSerial(YearPart, RefMonth, 1)
DatRefAR(YearCnt, 3) = False 'Un Flag si trouvé, pour utilisation ultérieure
Next YearPart
' Init des valeurs
CntVal = 0
SumVal = 0
For RowN = 2 To SrcRng.Rows.Count
If IsDate(SrcRng(RowN, 1)) Then 'Peut être supprimé si c'est toujours une date
DDate = SrcRng(RowN, 1)
DYear = Year(DDate)
For YearCnt = 1 To UBound(DatRefAR, 1)
' Si l'année correspond
If DYear = DatRefAR(YearCnt, 1) Then
' et si la date est postérieure à la référence, sans reset déjà effectué
If DatRefAR(YearCnt, 3) = False And DDate >= DatRefAR(YearCnt, 2) Then
DatRefAR(YearCnt, 3) = True
CntVal = 0
SumVal = 0
SrcRng(RowN, 13) = "Reset"
Exit For
End If
End If
Next YearCnt
Debug.Print RowN, SrcRng(RowN, 1), DMonth
CntVal = CntVal + 1
SumVal = SumVal + SrcRng(RowN, 7)
SrcRng(RowN, 14) = SumVal / CntVal
End If
Next RowN
End Sub |
Partager