Sorting each row one by one with column headings

near

New Member
Joined
Mar 15, 2012
Messages
22
Hi,
I have 100*374 data. With 374 columns, I want to sort each row of my data in descending order but keeping in mind that the respective column heading should also be sorted with the data.
For Example: BMW, Ferrari, Audi and Honda are the column headings in the first row and the other two rows are the data.


BMW......Ferrari.....Audi......Honda
32..........42........ 17..........19
50..........39.........40..........25


Now, Each Row should be sorted each time with their column headings


Ferrari..........BMW...........Honda.........Audi
42................32...............19.............17


BMW........Audi........Ferrari.......Honda
50............40.......... 39..............25




Any help will be greatly appreciated.


Note: I have places the same question on http://www.mrexcel.com/forum/newthread.php?do=newthread&f=10
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Hi c_m_s_jr,
What do you mean by this statement "I would think you would need a new column header above each row of data."?

New column means?
 
Upvote 0
Hi,
I have 100*374 data. With 374 columns, I want to sort each row of my data in descending order but keeping in mind that the respective column heading should also be sorted with the data.
For Example: BMW, Ferrari, Audi and Honda are the column headings in the first row and the other two rows are the data.


BMW......Ferrari.....Audi......Honda
32..........42........ 17..........19
50..........39.........40..........25


Now, Each Row should be sorted each time with their column headings


Ferrari..........BMW...........Honda.........Audi
42................32...............19.............17


BMW........Audi........Ferrari.......Honda
50............40.......... 39..............25




Any help will be greatly appreciated.


Note: I have places the same question on http://www.mrexcel.com/forum/newthread.php?do=newthread&f=10


This assumes your data is on Sheet1 and the results will be at Sheet2?

Code:
Sub near()
Dim i As Long
Dim lr As Long
Dim ws As Worksheet

Set ws = Sheets("Sheet1")

lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

For i = lr To 2 Step -1

    Sheets("Sheet2").Range("A1:A3").EntireRow.Insert SHIFT:=xlDown
    ws.Activate
    Rows("1:1").Copy Sheets("Sheet2").Range("A1")
    Range("A" & i).EntireRow.Copy Sheets("Sheet2").Range("A2")
    Sheets("Sheet2").Activate
    Rows("1:2").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
        DataOption1:=xlSortNormal
    ws.Activate
    
Next i
  
End Sub
 
Upvote 0
Thanks for your solution....
But I want the sorted result on the same sheet (not on sheet2) under the main data (one line after each time)
 
Upvote 0
Probably much better ways, this still uses Sheet2 as a helper.

Code:
Sub near()
Dim i As Long
Dim lr As Long
Dim ws As Worksheet

Set ws = Sheets("Sheet1")

lr = ws.Cells(Rows.Count, 1).End(xlUp).Row

For i = lr To 2 Step -1

    Sheets("Sheet2").Range("A1:A3").EntireRow.Insert SHIFT:=xlDown
    ws.Activate
    Rows("1:1").Copy Sheets("Sheet2").Range("A1")
    Range("A" & i).EntireRow.Copy Sheets("Sheet2").Range("A2")
    Sheets("Sheet2").Activate
    Rows("1:2").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlDescending, Header:=xlGuess, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlLeftToRight, _
        DataOption1:=xlSortNormal
    ws.Activate
    
Next i

Sheets("Sheet2").Activate
Sheets("Sheet2").UsedRange.Cut ws.Range("A" & Rows.Count).End(3)(2)
  
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,727
Messages
6,174,139
Members
452,546
Latest member
Rafafa

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