Trier un tableau vba
Dan457
-
pijaku Messages postés 13513 Date d'inscription Statut Modérateur Dernière intervention -
pijaku Messages postés 13513 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour,
j'ai défini un tableau avec la fonction type et j'aimerais trier ce tableau sur la premiere valeur du type.
j'aimerais trier ce tableau (ascending) en utilisant le composant department.
Merci pour votre aide
j'ai défini un tableau avec la fonction type et j'aimerais trier ce tableau sur la premiere valeur du type.
Type t_Out_Array
Cost_Center As String
NT_Account As String
Last_Name As String
First_Name As String
Group As String
Util_Status As String
Department As String
End Type
Public a_Out_Array(5000) As t_Out_Array
j'aimerais trier ce tableau (ascending) en utilisant le composant department.
Merci pour votre aide
1 réponse
-
Bonjour,
Un exemple de tri rapide (2 colonnes) à adapter à N colonnes (auteurs: Zon & Ti) par récursivité
Pour un tableau à 2 dimensions, pratique pour trier une plage de cellules par exemple:
T=range("A1:C50000").value
'On veut trier le tableau sur la colonne B:
Trimulti T,2,lbound(T),Ubound(T)
Sub TriMulti(Tablo, Col As Byte, Min&, Max&) 'ZOn
Dim I&, J&, K&, M, Chaine
I = Min
J = Max
M = Tablo((Min + Max) / 2, Col)
While (I <= J)
While (Tablo(I, Col) < M And I < Max)
I = I + 1
Wend
While (M < Tablo(J, Col) And J > Min)
J = J - 1
Wend
If (I <= J) Then
For K = LBound(Tablo, 2) To UBound(Tablo, 2)
Chaine = Tablo(I, K)
Tablo(I, K) = Tablo(J, K)
Tablo(J, K) = Chaine
Next K
I = I + 1
J = J - 1
End If
Wend
If (Min < J) Then TriMulti Tablo, Col, Min, J
If (I < Max) Then TriMulti Tablo, Col, I, Max
End Sub
-
Bonjour Michel,
Je la garde celle-ci.
Très efficace, rapide, claire, précise...
Impeccable pour trier des données.
Testée sur mon pc, elle met moins de 6 secondes pour le tri suivant :t = Range("A1:L50000").Value TriMulti t, 2, LBound(t), UBound(t)
What else?
Merci et bonne journée- salut Frank
OK, mais je n'ai fait qu'un copier coller !
petit supplément gratuit pour un tableau à 1 dimension auteur: Ti
Sub TrieTableau(Deb As Long, Fin As Long)'Ti
Dim IndiceInf As Long, IndiceSup As Long
Dim Temp1, Pivot
IndiceInf = Deb
IndiceSup = Fin
Pivot = UCase(T((Deb + Fin) \ 2))
Do
While UCase(T(IndiceInf)) < Pivot
IndiceInf = IndiceInf + 1
Wend
While Pivot < UCase(T(IndiceSup))
IndiceSup = IndiceSup - 1
Wend
If IndiceInf <= IndiceSup Then
Temp1 = T(IndiceInf)
T(IndiceInf) = T(IndiceSup)
T(IndiceSup) = Temp1
IndiceInf = IndiceInf + 1
IndiceSup = IndiceSup - 1
End If
Loop Until IndiceInf > IndiceSup
If Deb < IndiceSup Then TrieTableau Deb, IndiceSup
If IndiceInf < Fin Then TrieTableau IndiceInf, Fin
End Sub
-