+ Reply to Thread
Results 1 to 5 of 5

Thread: Macro - sort columns

  1. #1
    Registered User
    Join Date
    03-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    38

    Macro - sort columns

    Hello,

    I wrote a Macro last night that worked and now it fails.

    It's the final part of a Macro and I want to sort the data in just columns A:B (has header rows).
    The number of rows will change depending on the dataset so I want it to be able to determine all rows (or to last row).

    I recorded a code to begin with and am now editing it however when it gets to this part, I get "Run-time error '9' Subscript out of range"...

    The code I have is:

    Columns("A:B").Select
            ActiveWorkbook.Worksheets("Sheet 1").sort.SortFields. _
            Clear
            ActiveWorkbook.Worksheets("Sheet 1").sort.SortFields. _
            Add Key:=Range("A2").End(xlDown), SortOn:=xlSortOnValues, Order:=xlAscending, _
            DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("Sheet 1").sort
            .SetRange Range("A:B")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    Any help would be much appreciated.

    Thank you in advance,

    Damien
    Last edited by damo_uk; 01-17-2012 at 09:36 AM. Reason: SOLVED

  2. #2
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,382

    Re: Macro - sort columns

    The code that you have is typical to excel 2007 onwards. You require the older version of the code that is used for 2003 (I assume it will also work for 2000). Code like this -
    Cells.Sort Key1:=Range("B2"), Order1:=xlAscending, Key2:=Range("E2") _
            , Order2:=xlAscending, Header:=xlYes, OrderCustom:=1, MatchCase:= _
            False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2 _
            :=xlSortNormal
    The subscript out of range error is usually associated with a tab name that does not match. Check if your sheet in the file is named as "Sheet 1" (with the space) or "Sheet1".
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Registered User
    Join Date
    03-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro - sort columns

    Thank you once again Arlette.

    I took the advice from your note - the tabs was labelled "Sheet1", not "Sheet 1". I changed this and it worked straight away! (I am using Excel '07).

    Much appreciate your quick help!

    Damien

  4. #4
    Forum Moderator arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    4,382

    Re: Macro - sort columns

    Please change your profile information to reflect 2007 thanks.
    Cheers,
    Arlette

    If I helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Registered User
    Join Date
    03-04-2009
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    38

    Re: Macro - sort columns

    Ooh - Thank you - I forgot to change it when I changed jobs!

    Cheers for the heads up!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0