Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim nom$, tablo, resu(), a, i&, dat$, n&
nom = UCase(Sh.Name)
With Sheets("COMMANDE")
If nom = UCase(.Name) Then Exit Sub
tablo = .UsedRange.Resize(, 5) 'matrice, plus rapide, sur 5 colonnes
End With
ReDim resu(1 To UBound(tablo), 1 To 5)
a = Array("LUNDI", "MARDI", "MERCREDI", "JEUDI", "VENDREDI", "SAMEDI", "DIMANCHE") 'liste classée
For i = 1 To UBound(tablo)
If UCase(Trim(tablo(i, 1))) = "DATE" Then dat = UCase(Trim(tablo(i, 2)))
If UCase(Trim(tablo(i, 1))) = nom Then
n = n + 1
resu(n, 1) = Application.Match(dat, a, 0) 'nombre de 1 à 7
resu(n, 2) = dat
resu(n, 3) = tablo(i, 2) 'produits
resu(n, 4) = tablo(i, 4) 'quantités
resu(n, 5) = tablo(i, 5) 'remarques
End If
Next
'---restitution---
If Sh.FilterMode Then Sh.ShowAllData 'si la feuille est filtrée
With Sh.[A2] '1ère cellule de restitution, à adapter
If n Then
Application.ScreenUpdating = False
.EntireColumn.Insert 'insère une colonne A auxiliaire
With .Cells(1, 0).Resize(n, 5)
.Value = resu
.Sort .Cells(1), xlAscending, , .Cells(1, 3), xlAscending, Header:=xlNo 'tri sur les dates/nombres puis sur les produits
.Borders.Weight = xlThin 'bordures
.Cells(1).EntireColumn.Delete 'supprime la colonne auxiliaire
End With
End If
.Offset(n).Resize(Sh.Rows.Count - n - .Row + 1, 4).Delete xlUp 'RAZ en dessous
End With
Sh.Columns.AutoFit 'ajuste les largeurs
With Sh.UsedRange: End With 'actualise la barre de défilement verticale
End Sub