Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim nom$, s As Boolean, r As Range, n, v$, i
nom = Sh.Name
If Val(nom) = 0 Then Exit Sub
s = Me.Saved 'mémorise l'état
Application.ScreenUpdating = False
Sh.[O4] = ""
Sh.[T5] = Date 'date du jour
Sh.[AA:AB] = "" 'RAZ des colonnes auxiliaires du comptage pour repérer les erreurs
On Error Resume Next
Set r = Sheets("Nomenclature").[L:L].SpecialCells(xlCellTypeConstants)
On Error GoTo 0
If Not r Is Nothing Then
For Each r In r
If CStr(r) = nom Then
n = n + 1
Sh.Cells(n + 6, 1) = r(1, 3)
Sh.Cells(n + 6, 2).Resize(, 5) = r(1, -4).Resize(, 5).Value
Sh.Cells(n + 6, "AB") = r(1, 2)
Sh.Cells(n + 6, "AA") = "=COUNTIF(C[1],RC[1])" 'comptage
End If
Next r
'---détermination de la valeur la plus fréquente pour O4---
If n Then
v = CStr(Application.VLookup(Application.Max(Sh.[AA:AA]), Sh.[AA:AB], 2, 0))
Sh.[O4] = v
End If
'---effacement des lignes indésirables---
For i = 1 To n
If CStr(Cells(i + 6, "AB")) <> v Then Cells(i + 6, 1).Resize(, 6) = ""
Next i
'---tri sur la colonne A---
Sh.Cells(6, 1).Resize(n + 1, 6).Sort Sh.Cells(6, 1), xlAscending, Header:=xlYes
End If
Sh.Range("A" & n + 7 & ":F" & Sh.Rows.Count) = "" 'RAZ en-dessous
If s Then Me.Saved = True 'évite l'invite à la fermeture si pas d'autres modifications
End Sub