• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Adding 4th Sort Field

I'm needing to tweak codes that I found on the web that I've slightly changed to suit.

The code allows for sorting a data range by double-clicking header cells.  The original code allowed up to 3 sort keys -- which was sufficient until now.  I'd like to expand it to allow 4 or maybe more sort keys.  Played with expanding the code to 4 but it will not run.

Please see attached code, which has my attempt to expand the sort keys to 4.  Its hanging on the statement right below Case 4.  Please help.  

The original code, I'm sure was written & thankfully made available by an expert, dates back to 2004.  So if there is a more 'enhanced' solution I'd be glad to 'upgrade' - provided its not too complex for a beginner to debug/maintain.

Thanks in advance.



(worksheet code)....
----------------------------------------------------------------------------------------

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Excel.Range, Cancel As Boolean)
    Dim MyTarget As Range, x As Variant
    
    Set ShRe = Worksheets("Reconciliation")
    Set MyTarget = Intersect(Target.Cells(1, 1), ShRe.Range("HeaderArea"))
    
    If Sheets("Reconciliation").Range("BX2") > 0 Then
'   Formulas already present
        Range("E12").Select
        MsgBox "*** NOTE:  You cannot do a sort while Subtotals / Totals are present ***" & _
            vbCrLf & "" & _
            vbCrLf & "        *** Please reset your Sork keys, then you may Re-Sort ***", _
            Title:=" "
        Exit Sub
    Else
'   No formulas found
        If Not MyTarget Is Nothing Then
           If SortArr(0) < 4 Then
              x = SetArr(MyTarget.Column)
              
              Select Case SortArr(0)
              Case 1
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(1)), Order1:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
                 Range("BU2") = MyTarget
                 Range("BV2") = MyTarget.Column
                 Range("BZ2").Value = 1
              
              Case 2
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(1)), Order1:=xlAscending, _
                    Key2:=Cells(1, SortArr(2)), Order2:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortTextAsNumbers
                 Range("BU3") = MyTarget
                 Range("BV3") = MyTarget.Column
                 Range("BZ2").Value = 2
    
              Case 3
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(1)), Order1:=xlAscending, _
                    Key2:=Cells(1, SortArr(2)), Order2:=xlAscending, _
                    Key3:=Cells(1, SortArr(3)), Order3:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortTextAsNumbers, _
                    DataOption3:=xlSortTextAsNumbers
                 Range("BU4") = MyTarget
                 Range("BV4") = MyTarget.Column
                 Range("BZ2").Value = 3
    
              Case 4
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(1)), Order1:=xlAscending, _
                    Key2:=Cells(1, SortArr(2)), Order2:=xlAscending, _
                    Key3:=Cells(1, SortArr(3)), Order3:=xlAscending, _
                    Key4:=Cells(1, SortArr(4)), Order4:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, DataOption2:=xlSortTextAsNumbers, _
                    DataOption3:=xlSortTextAsNumbers, DataOption4:=xlSortTextAsNumbers
                 Range("BU5") = MyTarget
                 Range("BV5") = MyTarget.Column
                 Range("BZ2").Value = 4
    
              Case Else ' Defaults to sort on first column
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, 6), Order1:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
              End Select
              
           Else
              MsgBox "You have already reached the maximum of 4 sort criteria."
           End If
           Cancel = True ' Cancels default double-click behavior
        End If
    End If
End Sub


----------------------------------------------------------------------------------------
Function SetArr(SortByCol)
    Dim x As Long, Flag As Boolean
    Flag = False
    For x = 1 To 4
       If SortArr(x) = 0 Then
          SortArr(x) = SortByCol
          SortArr(0) = x ' Set criteria count
          Flag = True
          Exit For
       End If
    Next x
    SetArr = Flag
End Function


----------------------------------------------------------------------------------------
Private Sub ResetArr()
    Dim x As Long
    For x = 0 To 4
       SortArr(x) = 0
    Next x
----------------------------------------------------------------------------------------


(module code)...
----------------------------------------------------------------------------------------
Public SortArr(4)

Open in new window

0
dcnkmnarn
Asked:
dcnkmnarn
  • 3
1 Solution
 
Patrick MatthewsCommented:
Excel added a new object model for sorting in Excel 2007, to enable sorting on more than 3 columns.

I will research that, but in the meantime, the trick in 2003 and earlier was to do two sorts: sort first on the 4th column alone, then do another sort on 1, 2, and 3.  See below:

0
 
Patrick MatthewsCommented:
Ack, the code snippet didn't attach!


Case 4
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(4)), Order1:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers
                 ShRe.Range("DataArea").Sort Key1:=Cells(1, SortArr(1)), Order1:=xlAscending, _
                    Key2:=Cells(1, SortArr(2)), Order2:=xlAscending, _
                    Key3:=Cells(1, SortArr(3)), Order3:=xlAscending, _
                    Orientation:=xlTopToBottom, DataOption1:=xlSortTextAsNumbers, _
                    DataOption2:=xlSortTextAsNumbers, DataOption3:=xlSortTextAsNumbers
                 Range("BU5") = MyTarget
                 Range("BV5") = MyTarget.Column
                 Range("BZ2").Value = 4

Open in new window

0
 
Patrick MatthewsCommented:
With a little help from the macro recorder, I came up with this "new style" way to sort four columns.  It is based on sorting A1:D26, with headings in Row 1.


With ActiveWorkbook.Worksheets("Sheet1").Sort
        With .SortFields
            .Clear
            .Add Key:=Range("A2:A26"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("B2:B26"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("C2:C26"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Add Key:=Range("D2:D26"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        End With
        .SetRange Range("A1:D26")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

Open in new window

0
 
dcnkmnarnAuthor Commented:
Sweet!  That did the trick.  Thanks a bunch!

I did also use the macro recorder to help me, and did see the coding it yielded, but figured it would be over my abilities to incorporate that into the existing code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now