Sub Insertion()
'se lance par Ctrl+M
Dim ncol%, tablo, resu(), i&, n&, test As Boolean, j%
Application.ScreenUpdating = False
With ActiveSheet
If .FilterMode Then .ShowAllData 'si la feuille est filtrée
.Cells.FormatConditions.Delete 'supprime toutes les MFC
'---tableau des résultats---
With .UsedRange
ncol = .Columns.Count
If ncol < 3 Then ncol = 3
tablo = .Resize(, ncol) 'matrice, plus rapide
ReDim resu(1 To Rows.Count, 1 To ncol)
For i = 2 To UBound(tablo)
If tablo(i, 1) <> "" And tablo(i, 1) <> tablo(1, 1) And (tablo(i - 1, 1) <> tablo(1, 1) Or i = 2) Then 'les 3 lignes sont ignorées
n = n + 1
If i > 3 Then test = tablo(i - 3, 1) = ""
If tablo(i, 2) <> tablo(i - 1, 2) Or test Then
For j = 1 To ncol
resu(n + 1, j) = tablo(1, j) 'copie les en-têtes
Next j
resu(n + 2, 1) = tablo(i, 1)
resu(n + 2, 2) = tablo(i, 3)
n = n + 3
End If
For j = 1 To ncol
resu(n, j) = tablo(i, j)
Next j
End If
Next i
If n Then .Offset(1).Resize(n) = resu 'restitution
.Rows(2).Offset(n).Resize(Rows.Count - n - .Row).EntireRow.Delete 'RAZ en dessous
End With
'---mises en forme conditionnelles (MFC)---
With .UsedRange.Offset(1)
.FormatConditions.Add xlExpression, Formula1:="=" & .Cells(0, 1).Address(0, 1) & "="""""
.FormatConditions(1).Font.Bold = True 'gras
.FormatConditions(1).Interior.ColorIndex = 15 'gris
End With
With .UsedRange.Offset(2)
.FormatConditions.Add xlExpression, Formula1:="=" & .Cells(-1, 1).Address(0, 1) & "="""""
.FormatConditions(2).Font.Bold = True 'gras
.FormatConditions(2).Interior.ColorIndex = 48 'gris foncé
End With
End With
End Sub