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,081,693
Messages
5,360,668
Members
400,592
Latest member
katekoz

Some videos you may like

This Week's Hot Topics

  • VBA (Userform)
    Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
  • List box that changes fill color
    Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
  • Remove duplicates and retain one. Cross-linked cases
    Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
  • VBA Copy and Paste With Duplicates
    Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
  • Macro
    is it possible for a macro to run if the active cell value is different to the value above it
  • IF DATE and TIME
    I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...
Top