Function Couleur(r As Range)
For Each r In r
If r.Interior.ColorIndex = 10 Then Couleur = Couleur + 1 '10 = vert foncé
Next
End Function
Function TriCouleur(r As Range)
Application.Volatile
Dim nlig, ncol, a(), b(), c(), i
nlig = r.Rows.Count - 1
ncol = r.Columns.Count
ReDim a(1 To ncol)
ReDim b(1 To ncol)
ReDim c(1 To ncol)
For i = 1 To ncol
a(i) = r(1, i)
b(i) = Couleur(r(2, i).Resize(nlig))
c(i) = b(i) - i / 10000 'pour ne pas modifier l'ordre des ex aequo
Next
tri c, a, b, 1, ncol
ReDim c(1 To 2, 1 To ncol)
For i = 1 To ncol
c(1, i) = a(i)
c(2, i) = b(i)
Next
TriCouleur = c 'matrice
End Function
Sub tri(c, a, b, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = c((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While c(g) > ref: g = g + 1: Loop
Do While ref > c(d): d = d - 1: Loop
If g <= d Then
temp = c(g): c(g) = c(d): c(d) = temp
temp = a(g): a(g) = a(d): a(d) = temp
temp = b(g): b(g) = b(d): b(d) = temp
g = g + 1: d = d - 1
End If
Loop While g <= d
If g < droi Then Call tri(c, a, b, g, droi)
If gauc < d Then Call tri(c, a, b, gauc, d)
End Sub