VBA: Sort on two columns

gio123bg

Active Member
Joined
Feb 14, 2004
Messages
255
Hi,
I have the necessity to execute a macro for sorting a list of data on two columns.
More exactly, let's suppose to have this situation:

A 10/2/2008 FFFF GGGG HHH
B 01/3/2008 PPPP LLLLL NNNN
B 12/4/2008 XXXX JJJJJ PPPP
B 08/1/2008 HHHH SSSS IIIII
C 15/10/2008 AAA BBBB CCC

I need this sort:
A 10/2/2008 FFFF GGGG HHH
B 08/1/2008 HHHH SSSS IIIII
B 01/3/2008 PPPP LLLLL NNNN
B 12/4/2008 XXXX JJJJJ PPPP
C 15/10/2008 AAA BBBB CCC

Any suggestion will be well appreciated.

Thanks in advance for your kind support.

Regars,

Giovanni
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi,
as in my example, I need the necessity to sort both column 1 (A,B,C, ...) and column 2 (date grouped by sort in column 1) in my macro.

Any idea?
Thanks in advance.
Regards,

Giovanni
 
Upvote 0
Like this?

Code:
Sub Macro1()
'
' Macro1 Macro
' Macro recorded 22/10/2008 by apoulsom
'
'
    Range("A1:E5").Select
    Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Key2:=Range("B1") _
        , Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
        False, Orientation:=xlTopToBottom
End Sub
 
Upvote 0
Hello,

I'm a little new at VBA programming but I've been learning a lot just over the past few days. Anyway, I have a spreadsheet with about 9000 rows, but this number may vary.

I need a macro to find the column that has the word "Date" in row 1, and then sort that column Newest to Oldest. Also, it needs to exclude row 1 from the sort because row 1 is my headers.

I used the macro recorder, the code results are below. But I need to tweak it a little bit. Thanks for your help!

Sub sort_by_date()
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("B1"), _
SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:B17")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
 
Upvote 0
Does this work for you?

Code:
Sub Test()
    Dim c As Integer
    With ActiveSheet.Range("A1").CurrentRegion
        c = .Find(What:="Date", After:=.Cells(1, 1), LookIn:=xlFormulas, _
            LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
            MatchCase:=False).Column
            .Sort Key1:=.Cells(1, c), Order1:=xlDescending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    End With
End Sub
 
Upvote 0
Thanks. It looks like it's working. So this sorts every row in the spreadsheet by date, right? Just making sure.. There are columns to the right of the Date column, also. I just want to make sure that it sorts the entire spreadsheet, basically.
 
Upvote 0
It sorts A1's current region by the column headed Date. The current region is the range surrounded by blank rows and columns.
 
Upvote 0

Forum statistics

Threads
1,214,905
Messages
6,122,174
Members
449,071
Latest member
cdnMech

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