Private Sub Worksheet_Change(ByVal Target As Range)
Dim P As Range, nplage As Byte, lig&, mini As Byte, h&, i&, nombre&, n As Byte, v#, j&
Application.ScreenUpdating = False
Application.EnableEvents = False 'désactive les évènements
Set P = [O18:P27,O34:P43] 'plage à remplir
For nplage = 1 To P.Areas.Count
lig = 0
mini = IIf(nplage = 1, 5, 3)
P.Areas(nplage).ClearContents 'RAZ
With [BRIE_DES_MORINS] 'tableau structuré
.Sort .Columns(7), xlAscending, .Columns(6), , xlAscending, .Columns(10), xlAscending, Header:=xlYes 'tri sur 3 colonnes
h = Application.CountIf(.Columns(7), "077")
If h = 0 Then Exit Sub
With .Rows(Application.Match("077", .Cells(7), 0)).Resize(h)
For i = 1 To h
If .Cells(i, 6) <> .Cells(i - 1, 6) Then
If nplage = 1 Then
nombre = Application.CountIf(.Columns(6), .Cells(i, 6))
Else
nombre = Application.CountIfs(.Columns(6), .Cells(i, 6), .Columns(11), "F")
End If
If nombre >= mini Then
lig = lig + 1
Cells(lig, "AA") = .Cells(i, 6) 'colonne AA auxiliaire
If nplage = 1 Then
Cells(lig, "AB") = Application.Sum(.Cells(i, 10).Resize(mini)) 'colonne AB auxiliaire
Else
n = 0
v = 0
For j = i To h
If UCase(.Cells(j, 11)) = "F" Then _
v = v + .Cells(j, 10): n = n + 1: If n = mini Then Exit For
Next j
Cells(lig, "AB") = v 'colonne AB auxiliaire
End If
End If
End If
Next i
End With
End With
'---restitution---
If lig Then
[AA:AB].Sort Columns("AB"), xlAscending, Header:=xlNo
P.Areas(nplage) = [AA1:AB10].Value
End If
[AA:AB].ClearContents 'RAZ
Next nplage
Application.EnableEvents = True 'réactive les évènements
End Sub