mergesort in VBA

billyheanue

Board Regular
Joined
Jul 13, 2015
Messages
109
53147826

<tbody>
</tbody>

I have a one line table of 8 numbers, and am trying to write code so that the output is a one line table of 8 numbers - yet they are sorted from 1- 8.

is there any way to structure recursion in VB? any recommendations about solving this?
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Code:
Sub mergesort()
Dim Val1 As Integer
Dim val2 As Integer
Dim val3 As Integer
Dim val4 As Integer
Dim val5 As Integer
Dim val6 As Integer
Dim val7 As Integer
Dim val8 As Integer


Dim x As Integer
x = 3


Val1 = Cells(7, x).Value
val2 = Cells(7, x + 1).Value
val3 = Cells(7, x + 2).Value
val4 = Cells(7, x + 3).Value
val5 = Cells(7, x + 4).Value
val6 = Cells(7, x + 5).Value
val7 = Cells(7, x + 6).Value
val8 = Cells(7, x + 7).Value


If Val1 < val2 _
And Val1 < val2 _
And Val1 < val3 _
And Val1 < val4 _
And Val1 < val5 _
And Val1 < val6 _
And Val1 < val7 _
And Val1 < val8 _
Then Cells(8, x) = Val1 _
Else: MsgBox "These arent the droids you are looking for"






End Sub

Welp, got this far. Problem is this is only to evaluate the first number. Theres gotta be a faster way!
 
Upvote 0
There are a zillion sort routines in VBA, but how about just a simple formula?

Row\Col
A​
B​
C​
D​
E​
F​
G​
H​
I​
J​
2​
5​
3​
1​
4​
7​
8​
2​
6​
3​
1​
2​
3​
4​
5​
6​
7​
8​
A3: =SMALL($A$2:$H$2, COLUMNS($B1:B1))
 
Upvote 0
I'm trying to use this sort of exercise as a way to learn more VB, and I completely agree with you that using that formula would be easier.
 
Upvote 0

Forum statistics

Threads
1,216,503
Messages
6,131,020
Members
449,615
Latest member
Nic0la

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top