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,082,103
Messages
5,363,158
Members
400,720
Latest member
Pettel

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top