27. august 2002 - 11:10Der er
10 kommentarer og 1 løsning
Sortering af data i et Array med VBA
Jeg har et Array (4000,3), som Jeg skal sortere efter første række (4000,1). Jeg bruger en bubble sort, men det tager 100 år, er der en hurtigere måde??
Her er et quicksort modul du kan bruge (det er meget hurtigere end boblesort når der er mange data), men faktisk er det lige så hurtigt at oprette et midlertidig ark, smide dit array ind på det, bruge excel's egen sortering (lynende hurtigt) og derefter henter det tilbage
Sub Quicksort(values(), ByVal min As Long, ByVal max As Long)
Dim med_value As String Dim hi As Long Dim lo As Long Dim i As Long
' If the list has only 1 item, it's sorted. If min >= max Then Exit Sub
' Pick a dividing item randomly. i = min + Int(Rnd(max - min + 1)) med_value = values(i)
' Swap the dividing item to the front of the list. values(i) = values(min)
' Separate the list into sublists. lo = min hi = max Do ' Look down from hi for a value < med_value. Do While values(hi) >= med_value hi = hi - 1 If hi <= lo Then Exit Do Loop
If hi <= lo Then ' The list is separated. values(lo) = med_value Exit Do End If
' Swap the lo and hi values. values(lo) = values(hi)
' Look up from lo for a value >= med_value. lo = lo + 1 Do While values(lo) < med_value lo = lo + 1 If lo >= hi Then Exit Do Loop
If lo >= hi Then ' The list is separated. lo = hi values(hi) = med_value Exit Do End If
' Swap the lo and hi values. values(hi) = values(lo) Loop ' Loop until the list is separated.
' Recursively sort the sublists. Quicksort values, min, lo - 1 Quicksort values, lo + 1, max
her er lige et modul til til det med at bruge excels egen sortering
Sub WorksheetSort(list)
Dim First As Integer, Last As Long Dim i As Long Dim FirstCell As Range, LastCell As Range Dim CurrCell As Range, FillRange As Range Dim wshTemp As Worksheet Set wshTemp = Worksheets.Add
First = LBound(list, 1) Last = UBound(list, 1)
Set FirstCell = wshTemp.Cells(1, 1) Set LastCell = wshTemp.Cells(Last, 1) Set FillRange = wshTemp.Range(FirstCell, LastCell) Application.ScreenUpdating = False
' Transfer the array to worksheet FillRange.Value = list
' Sort the worksheet range FirstCell.CurrentRegion.Sort Key1:=FirstCell, Order1:=xlAscending, Orientation:=xlTopToBottom
' Transfer range back to the array and clear range For i = First To Last list(i, 0) = FirstCell.Offset(i - 1, 0) Next i Application.DisplayAlerts = False wshTemp.Delete Application.DisplayAlerts = True Application.ScreenUpdating = True End Sub
Sub Quicksort(values(), min, max) Dim med_value Dim hi As Long Dim lo As Long Dim i As Long
' If the list has only 1 item, it's sorted. If min >= max Then Exit Sub
' Pick a dividing item randomly. i = min + Int(Rnd(max - min + 1)) med_value = values(i, 1)
' Swap the dividing item to the front of the list. values(i, 1) = values(min, 1)
' Separate the list into sublists. lo = min hi = max Do ' Look down from hi for a value < med_value. Do While values(hi, 1) >= med_value hi = hi - 1 If hi <= lo Then Exit Do Loop
If hi <= lo Then ' The list is separated. values(lo, 1) = med_value Exit Do End If
' Swap the lo and hi values. values(lo, 1) = values(hi, 1)
' Look up from lo for a value >= med_value. lo = lo + 1 Do While values(lo, 1) < med_value lo = lo + 1 If lo >= hi Then Exit Do Loop
If lo >= hi Then ' The list is separated. lo = hi values(hi, 1) = med_value Exit Do End If
' Swap the lo and hi values. values(hi, 1) = values(lo, 1) Loop ' Loop until the list is separated.
' Recursively sort the sublists. Quicksort values, min, lo - 1 Quicksort values, lo + 1, max
okay bak men du må lige hjælpe mig lidt... Values() er = mit Array(4000,1) eller?... jeg har en textbox, og hvergang der sker en ændring skal der sorteres
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.