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.
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
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
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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