+ Reply to Thread
Results 1 to 4 of 4

The Sort Reference Is Not Valid

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-09-2006
    Posts
    109

    The Sort Reference Is Not Valid

    I have a tab with four different sets of similar data. Each pasted from a pivot table with a different item in filter selected. I need to sort each column desc, which is why i can't leave it in the pivot. I have four sets of similar code, only the starting point of the data sets changes. When running the macro, the first data set sorts perfectly. But when i get to the apply step in the second set of code, it tells me "Run time error 1004- The sort reference is not valid. Make sure that it's within the data you want to sort, and the first Sort By Box isn't the same or blank."

    Below are the first two sets of code
    sub test()
    first_start = 8
    second_start = 15
    'Sort First
        Set lastrow = Cells(Rows.Count, ConvertToLetter(Val(first_start))).End(xlUp)
        Range(ConvertToLetter(Val(first_start)) & "4").Select
        Selection.End(xlToRight).Select
        Range(ConvertToLetter(Val(first_start)) & lastrow.Row & ":" & ConvertToLetter(ActiveCell.Column) & lastrow.Row).Insert
        Selection.AutoFilter
    'account for column label
        num = 1
        Dim test As String
        Dim test2 As String
        Dim test3 As String
        Dim test4 As String
        test = ConvertToLetter(first_start + num) & "5:" & ConvertToLetter(first_start + num) & lastrow.Row - 1
        For Each risk In Range(ConvertToLetter(Val(first_start)) & "4:" & ConvertToLetter(ActiveCell.Column - 1) & "4")
                 
            ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort.SortFields.Add Key:=Range( _
            ConvertToLetter(first_start + num) & "5:" & ConvertToLetter(first_start + num) & lastrow.Row - 1), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
            
            num = num + 1
        Next risk
        test2 = ConvertToLetter(Val(first_start)) & "4:" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1
        With ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort
            .SetRange Range(ConvertToLetter(Val(first_start)) & "4:" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        Range(ConvertToLetter(Val(first_start)) & lastrow.Row - 1 & ":" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1).Delete
    
                
    'Sort Second
        Set lastrow = Cells(Rows.Count, ConvertToLetter(Val(second_start))).End(xlUp)
        Range(ConvertToLetter(Val(second_start)) & "4").Select
        Selection.End(xlToRight).Select
        Range(ConvertToLetter(Val(second_start)) & lastrow.Row & ":" & ConvertToLetter(ActiveCell.Column) & lastrow.Row).Insert
        Selection.AutoFilter
        Selection.AutoFilter
    'account for column label
        num = 1
     
        test3 = ConvertToLetter(second_start + num) & "5:" & ConvertToLetter(second_start + num) & lastrow.Row - 1
        For Each risk In Range(ConvertToLetter(Val(second_start)) & "4:" & ConvertToLetter(ActiveCell.Column - 1) & "4")
                 
            ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort.SortFields.Add Key:=Range( _
            ConvertToLetter(second_start + num) & "5:" & ConvertToLetter(second_start + num) & lastrow.Row - 1), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
            xlSortNormal
            
            num = num + 1
        Next risk
    
        test4 = ConvertToLetter(Val(second_start)) & "4:" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1
        With ActiveWorkbook.Worksheets(ActiveSheet.Name).Sort
            .SetRange Range(ConvertToLetter(Val(second_start)) & "4:" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
        Range(ConvertToLetter(Val(second_start)) & lastrow.Row - 1 & ":" & ConvertToLetter(ActiveCell.Column) & lastrow.Row - 1).Delete
        Range("A4").Select
    end sub
    
    
    Function ConvertToLetter(iCol As Integer) As String
       Dim iAlpha As Integer
       Dim iRemainder As Integer
       iAlpha = Int(iCol / 27)
       iRemainder = iCol - (iAlpha * 26)
       If iAlpha > 0 Then
          ConvertToLetter = Chr(iAlpha + 64)
       End If
       If iRemainder > 0 Then
          ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
       End If
    End Function
    I can’t for the life of me figure out why I’m getting that error. I threw in those test-test4 variables to help me, but as far as i can tell the outputs are what they should be. IE test matches test3 and test2 matches test4. The ending rows and starting columns are different, but looking at the data they match.

    Capture.PNG

    Any assistance would be appreciated.
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: The Sort Reference Is Not Valid

    No offense, I hope, but I found the code to be unnecessarily complex and confusing (especially with all the unneeded ConvertToLetter function calls, to the point where it was easier to just rewrite it rather than decipher it. Give this a try. It will work for any number of data sets (as long as they all start on the same row) and any number of columns within each set:

    Option Explicit
    
    Sub SortData()
    Dim EndRw As Long, BegCol As Long, EndCol As Long, CurCol As Long
    Const BegRw = 5
    
        If Cells(BegRw - 1, 1).Value = "" Then
            BegCol = Cells(BegRw - 1, 1).End(xlToRight).Column
        Else
            BegCol = 1
        End If
    
        Do Until BegCol > 100
            EndCol = Cells(BegRw - 1, BegCol).End(xlToRight).Column
            EndRw = Cells(BegRw, BegCol).End(xlDown).Row - 1
            With ActiveSheet.Sort
                .SortFields.Clear
                For CurCol = BegCol + 1 To EndCol - 1
                    .SortFields.Add Key:=Cells(BegRw, CurCol), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
                Next
                .SetRange Range(Cells(BegRw, BegCol), Cells(EndRw, EndCol))
                .Header = xlNo
                .Orientation = xlTopToBottom
                .Apply
            End With
            BegCol = Cells(BegRw - 1, EndCol).End(xlToRight).Column
        Loop
    End Sub
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  3. #3
    Forum Contributor
    Join Date
    01-09-2006
    Posts
    109

    Re: The Sort Reference Is Not Valid

    Wow that's so much more elegant than what I was trying. Worked like a charm. If you're ever in Birmingham, AL, I owe you a beer!!! Thanks so much.

  4. #4
    Forum Contributor
    Join Date
    01-09-2006
    Posts
    109

    Re: The Sort Reference Is Not Valid

    Oh, no offense taken. There's always a better way. I appreciate the rework. I'll take a look and see how it goes.

    Thanks again.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. The Sort reference is not valid - run time error
    By Josh_123456 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-23-2014, 09:14 AM
  2. Need help fixing macros due to Run-Time error 1004 Sort Reference is Not Valid
    By travisg in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-16-2013, 12:13 AM
  3. Sort Reference is Not Valid
    By nickv02 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-07-2012, 04:43 PM
  4. 'reference is not valid'
    By Tim in forum Excel General
    Replies: 12
    Last Post: 06-21-2006, 02:36 PM
  5. Reference is not valid
    By Steved in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-30-2006, 07:00 PM

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