Private Sub Worksheet_Activate()
Dim agent$, i&, x$, dat
Application.ScreenUpdating = False
If FilterMode Then ShowAllData 'si la feuille est filtrée
Range("K4:O" & Rows.Count) = "" 'RAZ
agent = Trim([O2])
On Error Resume Next 'si la feuille n'existe pas
t = Sheets(CStr([L2])).UsedRange.Resize(, 8) 'matrice, plus rapide
On Error GoTo 0
If agent = "" Or Not IsArray(t) Then GoTo 1
Set d = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(t)
x = Trim(t(i, 1))
If x <> "" And Trim(t(i, 8)) = agent Then
dat = t(i, 7)
If d.exists(x) Then
If IsDate(dat) Then If CDate(dat) > t(d(x), 7) Then d(x) = i 'mémorise la ligne
Else
d(x) = IIf(IsDate(dat), i, 0)
End If
End If
Next
If d.Count = 0 Then GoTo 1
With [K4].Resize(d.Count)
.Value = Application.Transpose(d.keys) 'Transpose est limitée à 65536 lignes
.Sort .Cells(1), xlAscending, Header:=xlNo 'tri
.Offset(, 1) = "=DerVal(K4,O$2,L$2,1,8,7,6)"
.Offset(, 2) = "=ROWS(M$4:M4)" 'index, il sert à quoi ???
.Offset(, 3) = "=DerVal(K4,O$2,L$2,1,8,7,7)"
'ajouter la formule pour la colonne O
End With
1 t = Empty 'RAZ
Set d = Nothing 'RAZ
With UsedRange: End With 'actualise la barre de défilement verticale
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [L2,O2]) Is Nothing Then Worksheet_Activate 'lance la macro
End Sub