Linking groups of cells in a row so they will sort together

S

Sam

Hello,

Never posted to the group before but have found lots of good advice.
I would like to find a way to link groups of cells in a row so they
will sort together when I sort in that row. My spreadsheet has 48
columns of data in each of the 15 rows. I would like to sort each row
by columns. In each row, the data in column 1 needs to stay next to
the data in column 2, column 3 needs to stay with column 4 and so on.
How do I write a macro that could sort by the value of the first
column in the pair in descending order and keep the value of the
second one in the pair in the column next to it. I have not written
macros before so I would appreciate as detailed advice as anyone is
willing to give.
Thanks,
Sam
 
D

David McRitchie

Hi Sam,
If you just mean keeping all the data on the row together
sorting the rows by Column A then Column B

Sub Macro35()
Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, _
Key2:=Range("B2"), Order2:=xlAscending, Header:=xlYes, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End Sub

hdr-A hdr-B hdr-C hdr-D
C 000 076 095
F 092 029 022
G 035 062 048
G 016 096 005
F 082 029 027
C 063 041 045

for a sorted version of

hdr-A hdr-B hdr-C hdr-D
C 000 076 095
C 063 041 045
F 082 029 027
F 092 029 022
G 016 096 005
G 035 062 048

The non programmed solution from which a macro was
recorded is:
1) Select all cells
2) Data, Sort,
Column A, Ascending
Column B, Ascending
my data has headers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top