Sort dependent on two columns

Per_

Board Regular
Joined
Sep 16, 2011
Messages
90
Hi,

I need some help to accomplish the below, two sort dependent two columns.


Before:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]



After:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
.
Code:
Option Explicit


Sub Macro1()


    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A1:A4"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1:B4"), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Sheet1").Sort
        .SetRange Range("A1:B4")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
End Sub
 
Upvote 0
Hi,

I need some help to accomplish the below, two sort dependent two columns.


Before:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]1[/TD]
[/TR]
</tbody>[/TABLE]



After:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Col A[/TD]
[TD]Col B[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]a[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]b[/TD]
[TD]2[/TD]
[/TR]
</tbody>[/TABLE]

http:/ /www.contextures.com/xlSort01.html
 
Last edited:
Upvote 0

Forum statistics

Threads
1,226,729
Messages
6,192,696
Members
453,747
Latest member
tylerhyatt04

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