re,
Utilise l'enregistreur de macro pour faire le tri à partir d'une colonne du tableau.
Sub Macro1()
'
' Macro1 Macro
'
'
Range("Tableau2[[#Headers],[% RO]]").Select
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields. _
Add2 Key:=Range("Tableau2[% RO]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A1").Select
End Sub
Ensuite modifier la macro pour que le tri se fasse sur la colonne sélectionnée. Cela grâce à la variable Col qui correspond à la cellule active
Sub Tri()
Dim Col As String
Col = ActiveCell.Value
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields. _
Add2 Key:=Range("Tableau2[" & Col & "]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
Range("Tableau2[[#Headers],[" & Col & "]]").Select
End Sub
Sur la feuille qui contient le tableau utiliser Worksheet_BeforeDoubleClick pour sélectionner la colonne voulue avec un double clic sur la ligne de titre.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("Tableau2[#Headers]")) Is Nothing Then 'Si l'on clique sur une cellule de la ligne de titre Alors
Dim Col As String 'Déclaration d'une variable permettant de cibler la cellule active
Col = ActiveCell.Value 'Affectation de la variable (cellule active)
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields.Clear 'On efface le filtre actif
ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort.SortFields. _
Add2 Key:=Range("Tableau2[" & Col & "]"), SortOn:=xlSortOnValues, Order _
:=xlAscending, DataOption:=xlSortNormal 'On filtre la colonne sélectionnée en ordre croissant
With ActiveWorkbook.Worksheets("Feuil2").ListObjects("Tableau2").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End If
Range("A1").Select
End Sub
Il faut remplacer dans la macro originelle
=Range("Tableau2[% RO]")
par
=Range("Tableau2[" & Col & "]")
Col représentant la cellule du titre de la colonne sur laquelle le filtre sera actif
Cordialement