mergesort in VBA

billyheanue

Board Regular
Joined
Jul 13, 2015
Messages
109
[TABLE="width: 512"]
<tbody>[TR]
[TD="width: 64, align: right"]5[/TD]
[TD="width: 64, align: right"]3[/TD]
[TD="width: 64, align: right"]1[/TD]
[TD="width: 64, align: right"]4[/TD]
[TD="width: 64, align: right"]7[/TD]
[TD="width: 64, align: right"]8[/TD]
[TD="width: 64, align: right"]2[/TD]
[TD="width: 64, align: right"]6[/TD]
[/TR]
</tbody>[/TABLE]

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?

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][td]
F​
[/td][td]
G​
[/td][td]
H​
[/td][td]
I​
[/td][td]
J​
[/td][/tr][tr][td]
2​
[/td][td]
5​
[/td][td]
3​
[/td][td]
1​
[/td][td]
4​
[/td][td]
7​
[/td][td]
8​
[/td][td]
2​
[/td][td]
6​
[/td][td][/td][td][/td][/tr]
[tr][td]
3​
[/td][td]
1​
[/td][td]
2​
[/td][td]
3​
[/td][td]
4​
[/td][td]
5​
[/td][td]
6​
[/td][td]
7​
[/td][td]
8​
[/td][td][/td][td]A3: =SMALL($A$2:$H$2, COLUMNS($B1:B1))[/td][/tr]
[/table]
 
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,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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