"The Sort reference is not valid"

JimSnyder

Board Regular
Joined
Feb 28, 2011
Messages
125
I have built a subroutine for doing multi column sorts. The problem is that the error on the subject line does not tell me where it is breaking. Can experts at sorts eyeball this and see if a problem pops out?

Code:
Option Explicit ' Must declare variables - clean programming technique


Public Sub MultiSort(ByRef currentSheet As Worksheet, _
    ByVal sortColOne As String, ByVal sortOneDirection As Integer, _
    ByVal sortColTwo As String, ByVal sortTwoDirection As Integer, _
    ByVal sortColThree As String, ByVal sortThreeDirection As Integer, _
    ByVal hasHeaders As Integer)


    Dim boxReturn As String
    Dim lastCol As Integer
    Dim LastRow As Long
    Dim usedRange As Range
    
    With currentSheet
        ' Get range of data
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set usedRange = .Range(.Cells(1, "A"), .Cells(lastCol & LastRow))
        
        ' Find how many sort keys were passed; check from left parameter to right parameter
        ' First parameter
        If Len(sortColOne) > 0 Then
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("" & sortColOne & ":" & sortColOne & ""), _
                SortOn:=xlSortOnValues, Order:=sortOneDirection, _
                DataOption:=xlSortNormal
                
            ' Second parameter
            If Len(sortColTwo) > 0 Then
                .Sort.SortFields.Add Key:=.Range("" & sortColTwo & ":" & sortColTwo & ""), _
                    SortOn:=xlSortOnValues, Order:=sortTwoDirection, _
                    DataOption:=xlSortNormal
                    
                ' Third parameter
                If Len(sortColThree) > 0 Then
                    .Sort.SortFields.Add Key:=.Range("" & sortColThree & ":" & sortColThree & ""), _
                        SortOn:=xlSortOnValues, Order:=sortThreeDirection, _
                        DataOption:=xlSortNormal
                End If
            End If
        Else
            boxReturn = MsgBox("First parameter is empty. Please enter first key information.", vbOK, "Empty Parameter")
        End If
        
        ' Do the sort
        With .Sort
            .SetRange usedRange
            .Header = hasHeaders
            .MatchCase = False
            .Orientation = xlSortRows
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub

The error '1004' is generated at the ".Apply".
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
The answer was simple. I was using the wrong orientation. Corrected code below:
Code:
Option Explicit ' Must declare variables - clean programming technique


Public Sub MultiSort(ByRef currentSheet As Worksheet, _
    ByVal sortColOne As String, ByVal sortOneDirection As Integer, _
    ByVal sortColTwo As String, ByVal sortTwoDirection As Integer, _
    ByVal sortColThree As String, ByVal sortThreeDirection As Integer, _
    ByVal hasHeaders As Integer)


    Dim boxReturn As String
    Dim lastCol As Integer
    Dim LastRow As Long
    Dim usedRange As Range
    
    With currentSheet
        ' Get range of data
        lastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
        Set usedRange = .Range(.Cells(1, "A"), .Cells(lastCol & LastRow))
        
        ' Find how many sort keys were passed; check from left parameter to right parameter
        ' First parameter
        If Len(sortColOne) > 0 Then
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Range("" & sortColOne & ":" & sortColOne & ""), _
                SortOn:=xlSortOnValues, Order:=sortOneDirection, _
                DataOption:=xlSortNormal
                
            ' Second parameter
            If Len(sortColTwo) > 0 Then
                .Sort.SortFields.Add Key:=.Range("" & sortColTwo & ":" & sortColTwo & ""), _
                    SortOn:=xlSortOnValues, Order:=sortTwoDirection, _
                    DataOption:=xlSortNormal
                    
                ' Third parameter
                If Len(sortColThree) > 0 Then
                    .Sort.SortFields.Add Key:=.Range("" & sortColThree & ":" & sortColThree & ""), _
                        SortOn:=xlSortOnValues, Order:=sortThreeDirection, _
                        DataOption:=xlSortNormal
                End If
            End If
        Else
            boxReturn = MsgBox("First parameter is empty. Please enter first key information.", vbOK, "Empty Parameter")
            Exit Sub
        End If
        
        ' Do the sort
        With .Sort
            .SetRange usedRange
            .Header = hasHeaders
            .MatchCase = False
[COLOR=#ff0000][B]            .Orientation = xlSortColumns[/B][/COLOR]
            .SortMethod = xlPinYin
            .Apply
        End With
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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