Bonjour,
Avec le code si dessous (trouvé sur le NET, Je ne sais plus où, désolé pour l'auteur),
je fais un tri d'un tableau VBA mais toujours de A à Z
Mais sur une des colonne je voudrais faire un tri inversé (de Z à A)
Est-ce que l'un d'entre vous peut m'aider ou m'orienter vers un sujet qui m'aiderai (Rien trouvé dans mes recherche)
Merci d'avance pour votre aide.
Patrick
Option Explicit
Dim tabloBDD
Sub TriBDD2Critère()
With FSourceTaxref
tabloBDD = .Cells(2, 1).Resize(.Cells(Rows.Count, 1).End(xlUp).Row - 1, 4)
End With
TriTabMult tabloBDD, 1, 3, 2
FSourceTaxref.[F1].Resize(UBound(tabloBDD), 4) = tabloBDD
End Sub
Option Compare Text
Sub TriTabMult(Tbl, Optional ColTri1, Optional Coltri2, Optional Coltri3)
If IsMissing(ColTri1) Then ColTri1 = 1
Dim clé() As String: ReDim clé(LBound(Tbl) To UBound(Tbl))
Dim idx() As Long: ReDim idx(LBound(Tbl) To UBound(Tbl))
Dim b(): ReDim b(LBound(Tbl) To UBound(Tbl), LBound(Tbl, 2) To UBound(Tbl, 2))
If IsNumeric(Tbl(1, ColTri1)) Or IsDate(Tbl(1, ColTri1)) Then Tri1 = "num"
If Not IsMissing(Coltri2) Then If IsNumeric(Tbl(1, Coltri2)) Or IsDate(Tbl(1, Coltri2)) Then Tri2 = "num"
If Not IsMissing(Coltri3) Then If IsNumeric(Tbl(1, Coltri3)) Or IsDate(Tbl(1, Coltri3)) Then Tri2 = "num"
For i = LBound(Tbl) To UBound(Tbl)
If Tri1 = "num" Then col1 = Format(Tbl(i, ColTri1), "0000000") Else col1 = Tbl(i, ColTri1)
If Not IsMissing(Coltri2) Then
If Tri2 = "num" Then col2 = Format(Tbl(i, Coltri2), "0000000") Else col2 = Tbl(i, Coltri2)
Else
col2 = ""
End If
If Not IsMissing(Coltri3) Then
If tri3 = "num" Then col3 = Format(Tbl(i, Coltri3), "0000000") Else col3 = Tbl(i, Coltri3)
Else
col3 = ""
End If
clé(i) = col1 & "_" & col2 & "_" & col3
idx(i) = i
Next i
Call QuickI(clé(), idx(), LBound(clé), UBound(clé))
For lig = LBound(clé) To UBound(clé)
For Col = LBound(Tbl, 2) To UBound(Tbl, 2): b(lig, Col) = Tbl(idx(lig), Col): Next Col
Next lig
For lig = LBound(clé) To UBound(clé)
For Col = LBound(Tbl, 2) To UBound(Tbl, 2): Tbl(lig, Col) = b(lig, Col): Next Col
Next lig
End Sub
Sub QuickI(clé() As String, index() As Long, gauc, droi) ' Quick sort
ref = clé((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While clé(g) < ref: g = g + 1: Loop
Do While ref < clé(d): d = d - 1: Loop
If g <= d Then
temp = clé(g): clé(g) = clé(d): clé(d) = temp
temp = index(g): index(g) = index(d): index(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call QuickI(clé, index, g, droi)
If gauc < d Then Call QuickI(clé, index, gauc, d)
End Sub