+ Reply to Thread
Results 1 to 3 of 3

Issue with Sorting a Range: Sort method of Range class failed

Hybrid View

  1. #1
    Registered User
    Join Date
    02-20-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2003
    Posts
    6

    Issue with Sorting a Range: Sort method of Range class failed

    Hi,

    I am trying to sort a range of data that starts in Column A and goes to Column AT (including this column). The data I want to sort starts on the second row as I have the first row as a header. The last row of my data is constantly changing as rows get added. The last row of data will be in FirstBlankRow as that is the row that was just filled with the data via my macro.

    Currently my code is adding my data to FirstBlankRow but it is not sorting the data afterwords. I keep getting Run-time error '1004': Sort method of Range class failed

    With Sheets("Models")
             .Range(.Cells(2, 1), .Cells(FirstBlankRow + 1, 46)).Sort key1:=.Cells(2, 1), Key2:=.Cells(2, 4) ', Key3:=.Cells(2, 2)
             End With
    Any help is greatly appreciated!

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Issue with Sorting a Range: Sort method of Range class failed

    Sub Sort()
    Dim LastRow As Long
    
        LastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
        Range("A1:AT" & LastRow).Select
        ActiveWorkbook.Worksheets("MASTER").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("MASTER").Sort.SortFields.Add Key:=Range("A2:A" & LastRow) _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveWorkbook.Worksheets("MASTER").Sort
            .SetRange Range("A1:AT" & LastRow)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        
    End Sub
    Change the coloured text to suit
    Red is the name of the worksheet
    Blue is the sort column
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    02-20-2013
    Location
    Mississauga, Canada
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Issue with Sorting a Range: Sort method of Range class failed

    Thanks for your response, I was about to try it when I realized the sheet I was trying to sort was protected with a password. I unprotected the sheet and my original code works perfectly now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.6.0 RC 1