function bubble sort three numbers


New Member
Oct 20, 2008
i need help any example post on bubble sort function not a sub that can sort three numbers and display it in any way via the three sorting cells or in the function cell using only single array dimension. also the function should use as double for all numbers to sort.

here the sample question from our work shop:
function using a form of the bubble sort algorithm which we
explored in a previous workshop. I suggest you start with the Matlab code that you wrote
previously and consider how you can convert it into VBA. You may wish to simplify the algorithm
since you only have three numbers to sort. If you set your outer loop to cycle through each pair of​
numbers twice this will ensure that you have correctly sorted the three numbers.

plz need help asap.


Well-known Member
Feb 17, 2003
This is the principle.
'- The method is to use a "pointer" and check its current position in the array.
'- If the current number is more than the next then switch their positions in the array.
'- If a position is changed then set a marker so the pointer goes through again.
'- The sort is complete when the pointer has gone through the array without change.
'- Brian Baulsom October 2008
Sub SORT_()
    Dim MyNumbers(3)
    Dim Pointer As Integer
    Dim Changed As Boolean
    Dim MyTemp
    MyNumbers(1) = 30
    MyNumbers(2) = 20
    MyNumbers(3) = 10
        Changed = False
        For Pointer = 1 To 3 - 1
            If MyNumbers(Pointer) > MyNumbers(Pointer + 1) Then
                MyTemp = MyNumbers(Pointer)
                MyNumbers(Pointer) = MyNumbers(Pointer + 1)
                MyNumbers(Pointer + 1) = MyTemp
                Changed = True
            End If
    Loop While Changed = True
    MsgBox (MyNumbers(1) & vbCr & MyNumbers(2) & vbCr & MyNumbers(3) & vbCr)
End Sub

Forum statistics

Latest member

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...