Bonjour à tous,
Voici une question sur laquelle de bute depuis quelques jours : Si je fais un tri par une macro avec « Données / Trier » je n’obtiens pas la même chose que si je fais un tri par une comparaison directe avec « < ».
Voici ma question : Comment faire un tri par la fonction SORT (ou une autre fonction) qui donne un résultat identique à un tri par comparaison directe ?
Je donne les exemples au bas message.
Merci par avance.
RéviAT
Liste d’entrée :
avoir
a fortiori
ô
à jeun
b
boire
aa
µ
£
île
a
Macro de tri par SORT :
Résultat :Code:
1
2
3
4
5
6
7
8
9
10 Sub TrieParTri() Application.ScreenUpdating = False ' Programme de trie par la fonction Tri Dim nbLignes As Long, noPlusPetit As Long, Truc As String Sheets("Liste").Select Columns("A:A").Select Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _ OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ DataOption1:=xlSortNormal End Sub
£
µ
a
a fortiori
à jeun
aa
avoir
b
boire
île
ô
Sous-programme de tri par comparaison :
Résultat :Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Sub TrieParComparaison() Application.ScreenUpdating = False ' Programme de trie de la première colonne (par comparaison) Dim nbLignes As Long, noPlusPetit As Long, Truc As String Sheets("Liste").Select nbLignes = Range("A65536").End(xlUp).Row For i = 2 To nbLignes - 1 Truc = Cells(i, 1).Formula noPlusPetit = i For j = i + 1 To nbLignes If Cells(j, 1).Formula < Truc Then Truc = Cells(j, 1).Formula noPlusPetit = j End If Next j If noPlusPetit <> i Then Truc = Cells(i, 1).Formula Cells(i, 1).Formula = Cells(noPlusPetit, 1).Formula Cells(noPlusPetit, 1).Formula = Truc End If Next i End Sub
a
a fortiori
aa
avoir
b
boire
£
µ
à jeun
île
ô