Sort All Columns in Excel 2003 with a Macro?

TechTank

Board Regular
Joined
Sep 5, 2011
Messages
92
Hi All,

I want to sort all columns on a sheet with a header row (In Row A1) in ascending order. The columns should all be sorted individually and not by any other cells. I have been using code similar to this:

Code:
    Columns("A:A").Select
    Selection.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Columns("B:B").Select
    Selection.Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal
    Columns("C:C").Select
    Selection.Sort Key1:=Range("C2"), Order1:=xlAscending, Header:=xlYes, _
        OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
        DataOption1:=xlSortNormal

This does work for each column I specify in the macro but I'd like to extend the range to the last column and the last row if possible so I don't have to add all the columns in by hand.

Any help is greatly appreciated.

Thank you for your time,

Mark.
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try:

Code:
Sub Test()
    Dim Col As Range
    For Each Col In ActiveSheet.UsedRange.Columns
        Col.Sort Key1:=Col.Cells(2, 1), Order1:=xlAscending, Header:=xlYes, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    Next Col
End Sub
 
Upvote 0
Hi Andrew,

That code is absolutely spot on!!! Thank you so much for a quick and fantastic response.

I'm learning every time I ask a question on here but hope to help someone out one day with the solutions people post for mine.

Warm regards and thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,259
Members
452,901
Latest member
LisaGo

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