Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

VBA Avec Autofilter dynamique sur les entêtes de colonnes

2 réponses
Avatar
Emile63
Bonjour Í  tous,

Je souhaite trier une base de données avec VBA, et l'enregistreur de macros donne quelque chose comme ça (Ci-dessous), mon problème c'est que comme la BDD augmente, par exemple le Range ("C4:C1000") eux restent figé Í  1000, comment faire en sorte qu'il augmente dynamiquement, ou que le tri se fasse sur la base de la colonne ou de l'entête, plutÍ´t qu'une plage définie...
Merci d'avance pour votre aide.

Application.Goto Reference:="MaBase"
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("A4:A10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("D4:D10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("E4:E10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("B4:B10000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

Bonne fin de journée,
Emile

2 réponses

Avatar
MichD
Le 02/07/22 Í  13:28, Emile63 a écrit :
Bonjour Í  tous,
Je souhaite trier une base de données avec VBA, et l'enregistreur de macros donne quelque chose comme ça (Ci-dessous), mon problème c'est que comme la BDD augmente, par exemple le Range ("C4:C1000") eux restent figé Í  1000, comment faire en sorte qu'il augmente dynamiquement, ou que le tri se fasse sur la base de la colonne ou de l'entête, plutÍ´t qu'une plage définie...
Merci d'avance pour votre aide.
Application.Goto Reference:="MaBase"
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("A4:A10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("D4:D10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("E4:E10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("B4:B10000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Bonne fin de journée,
Emile

Bonjour,
Sub test()
Dim Rg As Range, Ligne As Long
With ThisWorkbook.Worksheets("MesDonnées")
Ligne = .Range("A:E").Find("*", LookIn:=xlFormulas, _
SearchDirection:=xlPrevious).Row
Set Rg = .Range("A4:E" & Ligne)
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=Rg.Columns(1), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(4), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(5), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(2), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange Rg
.Header = False
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
MichD
Avatar
Emile63
Le Saturday, July 2, 2022 Í  8:30:25 PM UTC+2, MichD a écrit :
Le 02/07/22 Í  13:28, Emile63 a écrit :
Bonjour Í  tous,
Je souhaite trier une base de données avec VBA, et l'enregistreur de macros donne quelque chose comme ça (Ci-dessous), mon problème c'est que comme la BDD augmente, par exemple le Range ("C4:C1000") eux restent figé Í  1000, comment faire en sorte qu'il augmente dynamiquement, ou que le tri se fasse sur la base de la colonne ou de l'entête, plutÍ´t qu'une plage définie...
Merci d'avance pour votre aide.
Application.Goto Reference:="MaBase"
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("A4:A10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("D4:D10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("E4:E10000"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort.SortFields.Add2 Key:=Range("B4:B10000"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("MesDonnées").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Bonne fin de journée,
Emile
Bonjour,
Sub test()
Dim Rg As Range, Ligne As Long
With ThisWorkbook.Worksheets("MesDonnées")
Ligne = .Range("A:E").Find("*", LookIn:=xlFormulas, _
SearchDirection:=xlPrevious).Row
Set Rg = .Range("A4:E" & Ligne)
.Sort.SortFields.Clear
.Sort.SortFields.Add2 Key:=Rg.Columns(1), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(4), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(5), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add2 Key:=Rg.Columns(2), SortOn:=xlSortOnValues, _
Order:=xlDescending, DataOption:=xlSortNormal
With .Sort
.SetRange Rg
.Header = False
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
End Sub
MichD

Bonsoir MichD,
Parfait, comme toujours, merci beaucoup. :)
Bonne soirée,
Emile