1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
| Option Explicit
Sub tri_colA()
Dim oRng As Range
Dim oWksh As Worksheet
Dim i As Long, j As Long
Dim Valeur() As Double
Dim Valeur2()
Dim n As Long
Dim cnt As Long
Dim boucle As Integer
Dim oMax As Long
Dim oTemp() As Double
'***************************************************************************************'
' Récupération des valeurs de l'ensemble des feuilles '
'***************************************************************************************'
n = 1
ReDim Valeur(1 To 2, 1 To n)
For Each oWksh In ActiveWorkbook.Worksheets
'If oWksh.Name <> "Feuil4" Then
With oWksh
Set oRng = .Range("A1")
For i = 0 To .Cells(Rows.Count, 1).End(xlUp).Row - 1
If IsNumeric(oRng.Offset(i, 0)) Then
ReDim Preserve Valeur(1 To 2, 1 To n)
Valeur(1, n) = oRng.Offset(i, 0)
Valeur(2, n) = oRng.Offset(i, 1)
n = n + 1
Else
MsgBox "Une valeur en colonne A n'est pas numérique."
Exit Sub
End If
Next i
End With
'End If
Next oWksh
'Utilisation de QuickSort2 sur la "première colonne" afin de trier l'ensemble des données
QuickSort2 Valeur, 1, 1
'***************************************************************************************'
' Gestion du tri sur la deuxième colonne en fonction de la première '
'***************************************************************************************'
oMax = 1
boucle = 1
Do While oMax < UBound(Valeur, 2)
n = 1
'i =
Do While Valeur(1, oMax) = Valeur(1, oMax + 1)
oMax = oMax + 1
If oMax + 1 > UBound(Valeur, 2) Then
Exit Do
End If
Loop
ReDim oTemp(1 To 2, 1 To oMax - boucle + 1)
'Création du tableau temporaire
For i = LBound(oTemp, 1) To UBound(oTemp, 1)
For j = LBound(oTemp, 2) To UBound(oTemp, 2)
oTemp(i, j) = Valeur(i, j + boucle - 1)
Next j
Next i
'Utilisation de QuickSort2 sur un sous-ensemble du tableau principal (tableau temporaire)
QuickSort2 oTemp, 1, 2
'Recopie du tableau temporaire, trié
For i = LBound(oTemp, 1) To UBound(oTemp, 1)
For j = LBound(oTemp, 2) To UBound(oTemp, 2)
Valeur(i, j + boucle - 1) = oTemp(i, j)
Next j
Next i
boucle = oMax + 1
oMax = oMax + 1
Loop
'***************************************************************************************'
' Réalimentation des feuilles en fonction des ranges utilisées '
'***************************************************************************************'
boucle = 0
For Each oWksh In ActiveWorkbook.Worksheets
'If oWksh.Name <> "Feuil4" Then
With oWksh
cnt = .UsedRange.Columns(1).Cells.Count
For i = 1 To cnt
.Range("A1").Offset(i - 1, 0) = Valeur(1, i + boucle)
.Range("A1").Offset(i - 1, 1) = Valeur(2, i + boucle)
Next i
boucle = boucle + cnt
End With
'End If
Next oWksh
End Sub
' Sort a 2-dimensional array on either dimension
' Omit plngLeft & plngRight; they are used internally during recursion
' Sample usage to sort on column 4
' Dim MyArray(1 to 1000, 1 to 5) As Long
' QuickSort2 MyArray, 2, 4
' Dim MyArray(1 to 5, 1 to 1000) As Long
' QuickSort2 MyArray, 1, 4
Public Sub QuickSort2(ByRef pvarArray As Variant, plngDim As Long, plngCol As Long, Optional ByVal plngLeft As Long, Optional ByVal plngRight As Long)
Dim lngFirst As Long
Dim lngLast As Long
Dim varMid As Variant
Dim varSwap As Variant
Dim c As Long
Dim cMin As Long
Dim cMax As Long
cMin = LBound(pvarArray, plngDim)
cMax = UBound(pvarArray, plngDim)
Select Case plngDim
Case 1
If plngRight = 0 Then
plngLeft = LBound(pvarArray, 2)
plngRight = UBound(pvarArray, 2)
End If
lngFirst = plngLeft
lngLast = plngRight
varMid = pvarArray(plngCol, (plngLeft + plngRight) \ 2)
Do
Do While pvarArray(plngCol, lngFirst) < varMid And lngFirst < plngRight
lngFirst = lngFirst + 1
Loop
Do While varMid < pvarArray(plngCol, lngLast) And lngLast > plngLeft
lngLast = lngLast - 1
Loop
If lngFirst <= lngLast Then
For c = cMin To cMax
varSwap = pvarArray(c, lngFirst)
pvarArray(c, lngFirst) = pvarArray(c, lngLast)
pvarArray(c, lngLast) = varSwap
Next
lngFirst = lngFirst + 1
lngLast = lngLast - 1
End If
Loop Until lngFirst > lngLast
If plngLeft < lngLast Then QuickSort2 pvarArray, plngDim, plngCol, plngLeft, lngLast
If lngFirst < plngRight Then QuickSort2 pvarArray, plngDim, plngCol, lngFirst, plngRight
Case 2
If plngRight = 0 Then
plngLeft = LBound(pvarArray, 1)
plngRight = UBound(pvarArray, 1)
End If
lngFirst = plngLeft
lngLast = plngRight
varMid = pvarArray((plngLeft + plngRight) \ 2, plngCol)
Do
Do While pvarArray(lngFirst, plngCol) < varMid And lngFirst < plngRight
lngFirst = lngFirst + 1
Loop
Do While varMid < pvarArray(lngLast, plngCol) And lngLast > plngLeft
lngLast = lngLast - 1
Loop
If lngFirst <= lngLast Then
For c = cMin To cMax
varSwap = pvarArray(lngFirst, c)
pvarArray(lngFirst, c) = pvarArray(lngLast, c)
pvarArray(lngLast, c) = varSwap
Next
lngFirst = lngFirst + 1
lngLast = lngLast - 1
End If
Loop Until lngFirst > lngLast
If plngLeft < lngLast Then QuickSort2 pvarArray, plngDim, plngCol, plngLeft, lngLast
If lngFirst < plngRight Then QuickSort2 pvarArray, plngDim, plngCol, lngFirst, plngRight
End Select
End Sub |
Partager