Active Range to an array, sort the array and display content.

jbfuentes

New Member
Joined
Aug 26, 2017
Messages
1
Please help! I want to put a two dimensional selected range values into a single column array. Then sort the array values in ascending order. Then display the content of the sorted array.
This is VBA and Excel. Thanks!

Sub SortArray()

Dim rangearray() As Variant
Dim SourceRange As Range
Set SourceRange = Selection.CurrentRegion
rangearray = SourceRange.Value

'Call function to sort rangearray
Call Sortarray(rangearray)

End Sub

Sub Sortarray(MyArray() As Variant)

Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim list As String

First = LBound(MyArray)
Last = UBound(MyArray)
For i = First To Last - 1
For j = i + 1 To Last
If MyArray(i) > MyArray(j) Then
Temp = MyArray(j)
MyArray(j) = MyArray(i)
MyArray(i) = Temp
End If
Next j
Next i

For i = 1 To UBound(MyArray)
list = list & vbCrLf & MyArray(i)
Next
MsgBox list

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Hi & welcome to MrExcel
Try this
Code:
Sub Sortarray()

Dim rangearray() As Variant
Dim SourceRange As Range
Dim Cl As Range
Dim Cnt As Long

Set SourceRange = Selection.CurrentRegion
For Each Cl In SourceRange
    Cnt = Cnt + 1
    ReDim Preserve rangearray(1 To Cnt)
    rangearray(Cnt) = Cl
Next Cl
'Call function to sort rangearray
Call Sortarray2(rangearray)

End Sub

Sub Sortarray2(MyArray() As Variant)

Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim j As Integer
Dim Temp As String
Dim list As String

First = LBound(MyArray)
Last = UBound(MyArray)
For i = First To Last - 1
For j = i + 1 To Last
If MyArray(i) > MyArray(j) Then
Temp = MyArray(j)
MyArray(j) = MyArray(i)
MyArray(i) = Temp
End If
Next j
Next i

For i = 1 To UBound(MyArray)
list = list & vbCrLf & MyArray(i)
Next
MsgBox list

End Sub
 
Last edited:
Upvote 0
Perhaps this, Initial array in columns "A & B"
Code:
[COLOR=navy]Sub[/COLOR] Sortarray()
[COLOR=navy]Dim[/COLOR] n [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] Ac [COLOR=navy]As[/COLOR] [COLOR=navy]Long,[/COLOR] c [COLOR=navy]As[/COLOR] [COLOR=navy]Long[/COLOR]
[COLOR=navy]Dim[/COLOR] rangearray() [COLOR=navy]As[/COLOR] Variant
[COLOR=navy]Dim[/COLOR] SourceRange [COLOR=navy]As[/COLOR] Range
[COLOR=navy]Set[/COLOR] SourceRange = Selection.CurrentRegion
rangearray = SourceRange.Value
ReDim Ray(1 To UBound(rangearray) * 2)
[COLOR=navy]For[/COLOR] Ac = 1 To 2
    [COLOR=navy]For[/COLOR] n = 1 To UBound(rangearray, 1)
        c = c + 1
        Ray(c) = rangearray(n, Ac)
    [COLOR=navy]Next[/COLOR] n
[COLOR=navy]Next[/COLOR] Ac
Call Sortarray(Ray)
 [COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
 [COLOR=navy]Sub[/COLOR] Sortarray(MyArray [COLOR=navy]As[/COLOR] Variant)
 [COLOR=navy]Dim[/COLOR] First [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
 [COLOR=navy]Dim[/COLOR] Last [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
 [COLOR=navy]Dim[/COLOR] i [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
 [COLOR=navy]Dim[/COLOR] j [COLOR=navy]As[/COLOR] [COLOR=navy]Integer[/COLOR]
 [COLOR=navy]Dim[/COLOR] Temp [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
 [COLOR=navy]Dim[/COLOR] list [COLOR=navy]As[/COLOR] [COLOR=navy]String[/COLOR]
 First = LBound(MyArray)
 Last = UBound(MyArray)
    [COLOR=navy]For[/COLOR] i = First To Last
        [COLOR=navy]For[/COLOR] j = i + 1 To Last
            [COLOR=navy]If[/COLOR] Val(MyArray(i)) > Val(MyArray(j)) [COLOR=navy]Then[/COLOR]
                Temp = MyArray(j)
                MyArray(j) = MyArray(i)
                MyArray(i) = Temp
            [COLOR=navy]End[/COLOR] If
        [COLOR=navy]Next[/COLOR] j
 [COLOR=navy]Next[/COLOR] i
'[COLOR=green][B] For i = 1 To UBound(MyArray)[/B][/COLOR]
'[COLOR=green][B] list = list & vbCrLf & MyArray(i)[/B][/COLOR]
'[COLOR=green][B] Next[/B][/COLOR]
 MsgBox Join(MyArray, vbLf)
[COLOR=navy]End[/COLOR] [COLOR=navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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