Trying to Sort under VBA Code?

Mackeral

Board Regular
Joined
Mar 7, 2015
Messages
249
Office Version
  1. 365
Platform
  1. Windows
The code below generates what I think are the proper keys and sort code but doesn't work.
Any idea why?
Code:
Sub Data_Sort(SEET, Row_Rng, Col1, _              
              Optional Col2 = "", _
              Optional Col3 = "", _
              Optional Col4 = "", _
              Optional Header_Xtrl As XlYesNoGuess = xlNo)
    ' Sort Data in a Sheet.
    ' If Col Number is Negative, sort it descending.    
    
    ' Note: Subroutine assumes that "Rng_Row" _
            begins with  the Label row.

    Prog = "Data_Sort"
    
    ' Check Row Range
    Ptr = InStr(Row_Rng, ":")
    If Ptr Then
        Row1 = Left(Row_Rng, Ptr - 1)
        Row2 = Mid(Row_Rng, Ptr + 1)
    Else
        Msg = """Row_Rng"" is not a valid Row Range."
        Call Msg_Err(Prog, Msg, Row_Rng)
    End If


    SHEET.Sort.SortFields.Clear
    
    Call Data_Sort_Arg(SHEET, 1, Row1, Row2, Col1)
    Call Data_Sort_Arg(SHEET, 2, Row1, Row2, Col2)
    Call Data_Sort_Arg(SHEET, 3, Row1, Row2, Co13)
    Call Data_Sort_Arg(SHEET, 4, Row1, Row2, Col4)
 
    
    With SHEET.Sort
        .SetRange Range(Row_Rng)
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    
End Sub ' Data_Sort()

' ==========================================================

Sub Data_Sort_Arg(SHEET As Worksheet, Arg_Nr, Row1, Row2, Column)
    ' Sorting changed.
    ' 5/18/18 Created. WML
    ' 9/14/18 Changed "Add" to A"Add2".WML
    ' 9/14/18 Reworked for new System Sort code. WML
    
    Prog = "Data_Sort_Arg"
    
    Sheeet_Name = SHEET.Name


    ' Check Columns
    If Column = "" Then
        Exit Sub
    
    ElseIf Not IsNumeric(Column) Then
        Msg1 = "Column Pointer for Argument """ & Arg_Nr & """ is not a number."
        Call Msg_Err(Prog, Msg1)
        
    ElseIf Column > 0 Then
        Sort_How = xlAscending
        
    ElseIf Column < 0 Then
        Column = -Column
        Sort_How = xlDescending
    
    Else ' Column = 0
        Msg1 = "Column Pointer for Argument 'Col'" & Ptr & " is '0'."
        Call Msg_Err(Prog, Msg1)
    End If
    
    Col = Col_Ptr(Column)
    Sort_Rng = Col & (Row1 + 1) & ":" &  Col &  Row2
    
    SHEET.Sort.SortFields.Add2 _
        Key:=Range(Sort_Rng), _
        SortOn:=(xlYes), _
        Order:=Sort_How, _
        DataOption:=(xlSortNormal)
        
End Sub ' Data_Sort_Arg

See the next post for more example.
And thanks for the help.
Mac
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Here is the Macro developed from doing the sort from Excel.
You can see the code it developes, and it is the same code
that my Data_Sort produces in the previous post.
Code:
    Rows("1:50").Select
    Set SHEET = Sheets("Attendance")
    
    SHEET.Sort.SortFields.Clear
    
    SHEET.Sort.SortFields.Add2 _
        Key:=Range("B2:B50"), _
        SortOn:=(xlSortOnValues), _
        Order:=(xlDescending), _
        DataOption:=(xlSortNormal)
        
    SHEET.Sort.SortFields.Add2 _
        Key:=Range("A2:A50"), _
        SortOn:=(xlSortOnValues), _
        Order:=(xlAscending), _
        DataOption:=(xlSortNormal)
        
    With SHEET.Sort
        .SetRange Range("A1:I50")
        .Header = (xlYes)
        .MatchCase = False
        .Orientation = (xlTopToBottom)
        .SortMethod = xlPinYin
        .Apply
    End With

Mac
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

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