Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NbPaq As Long, TLPaq() As Long, Cel As Range, N As Long, LFin As Long
NbPaq = WorksheetFunction.CountIf(Columns("B"), "BUREAU")
ReDim TLPaq(1 To NbPaq)
Set Cel = Columns("B").Find(What:="BUREAU", LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=True, SearchFormat:=False)
For N = 1 To UBound(TLPaq)
TLPaq(N) = Cel.Row
Set Cel = Columns("B").FindNext(After:=Cel)
Next N
With Me.UsedRange: LFin = .Rows(.Rows.Count).Row: End With
For N = UBound(TLPaq) To 1 Step -1
TraiterPaquet [B:AD].Rows(TLPaq(N) - 1).Resize(LFin - TLPaq(N) + 2).Cells, Target
LFin = TLPaq(N) - 3: Next N
ActiveWindow.DisplayZeros = False
End Sub
Private Sub TraiterPaquet(ByVal RngPaq As Range, ByVal Cible As Range)
Dim Total As Double, MaxPF As Double, NbLig As Long, DifLigs As Long, RngLig As Range, R9$, R12$
If Intersect(RngPaq, Cible) Is Nothing Then Exit Sub
Total = RngPaq(3, 2).Value
MaxPF = RngPaq(5, 2).Value
NbLig = Int(Total / MaxPF): If NbLig * MaxPF < Total Then NbLig = NbLig + 1
If NbLig = 0 Then NbLig = 1
DifLigs = RngPaq.Rows.Count - 5 - NbLig
Application.EnableEvents = False
If DifLigs < 0 Then
RngPaq.Rows(RngPaq.Rows.Count).Resize(-DifLigs).Insert xlShiftDown, xlFormatFromLeftOrAbove
ElseIf DifLigs > 0 Then
RngPaq.Rows(6).Resize(DifLigs).Delete xlShiftUp
End If
Set RngLig = RngPaq.Rows(6).Resize(NbLig).Cells
Application.ScreenUpdating = False
With RngLig.Borders(xlEdgeBottom): .LineStyle = xlContinuous: .Weight = xlMedium: End With
With RngLig.Borders(xlInsideHorizontal): .LineStyle = xlContinuous: .Weight = xlThin: End With
Application.Calculation = xlCalculationManual
R9 = "R" & RngPaq.Rows(3).Row
R12 = "R" & RngPaq.Rows(6).Row
RngLig.FormulaR1C1 = "=MIN(MAX(RC3-SUM(RC4:RC[-1]),0),MAX(" & R9 & "C-SUM(" & R12 & "C:R[-1]C),0))"
RngLig.Columns(3).FormulaR1C1 = "=MIN(MAX(RC3,0),MAX(" & R9 & "C-SUM(" & R12 & "C:R[-1]C),0))"
RngLig.Rows(1).FormulaR1C1 = "=MIN(MAX(RC3-SUM(RC4:RC[-1]),0)," & R9 & "C)"
RngLig(1, 3).FormulaR1C1 = "=MIN(RC3," & R9 & "C)"
RngLig.Columns(2).Value = MaxPF
If RngLig.Rows.Count > 1 Then
RngLig.Rows(RngLig.Rows.Count).FormulaR1C1 = "=" & R9 & "C-SUM(" & R12 & "C:R[-1]C)"
Else
RngLig.Rows(1).FormulaR1C1 = "=" & R9 & "C": End If
RngLig.Columns(1).FormulaR1C1 = "=""PF ""&ROW()-" & RngLig.Row - 1
Application.Calculation = xlCalculationAutomatic
' RngLig.Value = RngLig.Value
Application.EnableEvents = True
End Sub