Sort - each individual column in a range

MNpoker

Board Regular
Joined
Dec 15, 2003
Messages
154
Example: I have N columns each with X entires.

I want to be able to highlight (select) the range and have it sort each INDIVIDUAL column. (Ascending or desending would be a nice choice as would rows but I think I can figure that out once I have the basics)

Code:
5	52
425	2
45	4
45	3
54	5
So if I selct the following (and run the macro with this range selected.

The output would be:

Code:
5	2
45	3
45	4
54	5
425	52
I tried the record macro thing technique but it keeps entering in range values ("A2"), etc.

And I want to be able to select the ranges.

I tried this to no avail.

Sub SortMacro()

Dim A As Range
Dim B As Range

Set A = Application.Selection

NumColumns = A.Columns.Count
NumRows = A.Rows.Count

For N = 1 To NumColumns

Set B = Range(A.Cells(1, N), A.Cells(NumRows, N))

' Now sort B
' Listed as comment because it doesn't work. But here is what I'm trying to start with and getting errors.

'Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlGuess, _
' OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
' DataOption1:=xlSortNormal

' It seems B is now a value not a specified range??

Next N

End Sub


I promise I looked through the other 100 or so sorting threads but I don't see where this was addressed.

Thanks in advance for any help.
 

MNpoker

Board Regular
Joined
Dec 15, 2003
Messages
154
GOT IT!!

Sub SortMacroToptoBottomAscending()
'
' SortMacro
'
Dim A As Range

Set A = Application.Selection

A.Select
Do Until IsEmpty(ActiveCell)
Selection.Sort Key1:=ActiveCell, Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Selection.Offset(0, 1).Select
Loop
End Sub
 

Forum statistics

Threads
1,077,829
Messages
5,336,647
Members
399,094
Latest member
Learner2019

Some videos you may like

This Week's Hot Topics

Top