- Joined
- Jul 26, 2010
- Messages
- 30
- Reaction score
- 0
I want to horizontally sort vertical groups (or columns) of data. I have found the following code to work perfectly for vertically sorting rows :
There are four columns and you can add as many rows as you want. Each function is assigned to one of the four buttons and so the buttons sort the rows starting from the second row, leaving the first row with criteria names intact. I've tried to adapt it to my need:
to work with four rows and however many columns, and it does work, except it doesn't exclude the first column with the criteria names; it sorts everything. Could someone please tell me what I'm doing wrong? Much obliged.
Code:
Sub Macroname()
Columns("A:D").Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
Sub Macrocity()
Columns("A:D").Sort Key1:=Range("c2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
Sub Macroage()
Columns("A:D").Sort Key1:=Range("d2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
Sub Macrounsort()
Columns("A:D").Sort Key1:=Range("a2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub
There are four columns and you can add as many rows as you want. Each function is assigned to one of the four buttons and so the buttons sort the rows starting from the second row, leaving the first row with criteria names intact. I've tried to adapt it to my need:
Code:
Sub Macroname()
Rows("1:4").Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End Sub
Sub Macrocity()
Rows("1:4").Sort Key1:=Range("B3"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End Sub
Sub Macroage()
Rows("1:4").Sort Key1:=Range("B4"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End Sub
Sub Macrounsort()
Rows("1:4").Sort Key1:=Range("B1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight
End Sub
to work with four rows and however many columns, and it does work, except it doesn't exclude the first column with the criteria names; it sorts everything. Could someone please tell me what I'm doing wrong? Much obliged.