+ Reply to Thread
Results 1 to 4 of 4

Thread: Sort: Run-time error '1004'

  1. #1
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Sort: Run-time error '1004'

    I get the following error when executing my macro:

    Run-time error '1004':
    Sort reference is not valid. Make sure that its within the data you want to sort, and the first sort box inst the same or blank

    The code fragment:

    'Get the range sum and record it
            For i = 3 To 12 'REVERB1
                Sum = 0
                For y = iStart To iEnd
                    Sum = Sum + Cells(y, i).Value
                Next y
                Sheet22.Cells(2, i - 1).Value = Sum
            Next i
            'Sort
            Sheet22.sort.SortFields.Add Key:=Range("B2:K2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With Sheet22.sort
                .SetRange Range("B1:K2")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlLeftToRight
                .SortMethod = xlPinYin
                .Apply
            End With
            
            Sheet3.Activate
            For i = 3 To 12 'REVERB2
                Sum = 0
                For y = iStart To iEnd
                    Sum = Sum + Cells(y, i).Value
                Next y
                Sheet22.Cells(4, i - 1).Value = Sum
            Next i
            'Sort
            Sheet22.sort.SortFields.Add Key:=Range("B4:K4"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With Sheet22.sort
                .SetRange Range("B3:K4")
                .Header = xlYes
                .MatchCase = False
                .Orientation = xlLeftToRight
                .SortMethod = xlPinYin
                .Apply
            End With
    The first ".Apply" line gets selected during debugging.

    Any ideas on whats wrong?

    Thanks in advance
    Last edited by nbuk; 07-16-2010 at 12:21 PM.

  2. #2
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Sort: Run-time error '1004'

    Initially, while still in the writing progress (i only had one "sort") this code executed without any errors. I do not think I changed anything to it, it just started to give errors.

    Please help?

  3. #3
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2007, 2010
    Posts
    25,777

    Re: Sort: Run-time error '1004'

    Perhaps
        With Sheet22.Sort
            .SortFields.Clear
            .SortFields.Add Key:=Range("B2:K2"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            .SetRange Range("B1:K2")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .Apply
        End With
    Microsoft MVP - Excel
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    06-25-2010
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2003
    Posts
    34

    Re: Sort: Run-time error '1004'

    Works great. Thanks!!

+ 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