ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Excel VBA - Sort a table with BubbleSort (https://www.excelbanter.com/excel-programming/290026-excel-vba-sort-table-bubblesort.html)

CGeorges[_3_]

Excel VBA - Sort a table with BubbleSort
 
Hello,

I need to sort a table with BubbleSort.

I know how the algorithm works:

For i = left To right - 1
If student_temp(i) student_temp(i + 1) Then
Call swap(i, i + 1)
End If


Private Sub swap
Dim hold As String

hold = student_temp(j)
student_temp(j) = student_temp(k)
student_temp(k) = hold


but I have no idea how this works on an Excel sheet. How do I access
the rows? Do I need an array?

I will appreciate any help.

Thanks in advance


---
Message posted from http://www.ExcelForum.com/


Bob Phillips[_6_]

Excel VBA - Sort a table with BubbleSort
 
Why do you need the bubble sort, why not use Excel's built-in sort?

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"CGeorges " wrote in message
...
Hello,

I need to sort a table with BubbleSort.

I know how the algorithm works:

For i = left To right - 1
If student_temp(i) student_temp(i + 1) Then
Call swap(i, i + 1)
End If


Private Sub swap
Dim hold As String

hold = student_temp(j)
student_temp(j) = student_temp(k)
student_temp(k) = hold


but I have no idea how this works on an Excel sheet. How do I access
the rows? Do I need an array?

I will appreciate any help.

Thanks in advance


---
Message posted from http://www.ExcelForum.com/




acw[_2_]

Excel VBA - Sort a table with BubbleSort
 
Hi

One way is to put the range data into an array, perform the sort, then put the data back again. Something like.

Sub aaa()
Dim arr As Variant
arr = Range("a1:f1").Value

For i = LBound(arr, 2) To UBound(arr, 2) - 1
For j = i + 1 To UBound(arr, 2)
If arr(1, i) arr(1, j) Then
holder = arr(1, i)
arr(1, i) = arr(1, j)
arr(1, j) = holder
End If
Next j
Next i

Range("a1:f1").Value = arr

End Sub

Tony

----- CGeorges wrote: -----

Hello,

I need to sort a table with BubbleSort.

I know how the algorithm works:

For i = left To right - 1
If student_temp(i) student_temp(i + 1) Then
Call swap(i, i + 1)
End If


Private Sub swap
Dim hold As String

hold = student_temp(j)
student_temp(j) = student_temp(k)
student_temp(k) = hold


but I have no idea how this works on an Excel sheet. How do I access
the rows? Do I need an array?

I will appreciate any help.

Thanks in advance


---
Message posted from http://www.ExcelForum.com/




All times are GMT +1. The time now is 07:15 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com