Option Compare Text 'la casse est ignorée lors du tri
Sub Trier_CSV()
Dim d As Object, maxi$, fichier$, x%, titres$, texte$, z, a(), n&, i&, b()
Set d = CreateObject("Scripting.Dictionary")
d.Comparemode = vbTextCompare 'la casse est ignorée
maxi = Chr(142) 'Chr(142) est plus grand que tout autre texte
fichier = ThisWorkbook.Path & "\Fichier CSV.csv"
x = FreeFile
Open fichier For Input As #x 'ouverture en lecture séquentielle
Line Input #x, titres '1ère ligne avec titres
While Not EOF(x)
Line Input #x, texte
z = Split(texte & ",", ",")(1) 'au moins 1 virgule
ReDim Preserve a(n)
If z = "" Then z = maxi
d(z) = d(z) + 1 'comptage
a(n) = z & Format(d(z), "00000") 'numérotation, maximum 99999 textes
ReDim Preserve b(n): b(n) = texte
n = n + 1
Wend
Close #x
tri a, b, 0, n - 1
'---création du fichier CSV trié---
fichier = Left(fichier, Len(fichier) - 4) & " trié.csv"
x = FreeFile
Open fichier For Output As #x 'ouverture en lecture séquentielle
Print #x, titres
For n = 0 To UBound(b)
Print #x, b(n)
Next n
Close #x
MsgBox "Le fichier CSV trié a été créé"
End Sub
Sub tri(a, b, gauc, droi) ' Quick sort
Dim ref, g, d, temp
ref = a((gauc + droi) \ 2)
g = gauc: d = droi
Do
Do While a(g) < ref: g = g + 1: Loop
Do While ref < a(d): d = d - 1: Loop
If g <= d Then
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(a, b, g, droi)
If gauc < d Then Call tri(a, b, gauc, d)
End Sub