VBA Sorting Inquiry

DonEB

Board Regular
Joined
Apr 26, 2016
Messages
133
Office Version
  1. 2019
Platform
  1. Windows
I've been slowly working my way thru an old excel spreadsheet which is loaded with macro's in an attempt to simplify them and make them more efficient. My attempt to simplify a sort has been unsuccessful and, despite many searches on the internet, I am unable to determine where I'm going wrong.

Here is my original code which does work.


VBA Code:
  Range("F43:I72").Select
    ActiveWorkbook.Worksheets("AllWeeks").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("AllWeeks").Sort.SortFields.Add Key:=Range( _
        "h43:h72"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                        
    ActiveWorkbook.Worksheets("AllWeeks").Sort.SortFields.Add Key:=Range( _
        "G43:G72"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                        
    ActiveWorkbook.Worksheets("AllWeeks").Sort.SortFields.Add Key:=Range( _
        "I43:I72"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal                                                                        
    With ActiveWorkbook.Worksheets("AllWeeks").Sort
        .SetRange Range("F43:I72")
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


Here is the new code I've been trying to debug and implement.

VBA Code:
Worksheets("AllWeeks").Sort.SortFields.Clear
Range("F43:I72").Sort Key1:=Range("H43"), Key2:=Range("G43"), Key3:=Range("I43"), Order1:=xlAscending, Order2:=xlAscending, Order3:=xlAscending, Header:=x1No


Any thoughts on what I'm doing wrong would be greatly appreciated.

Thanks,
Don
 
Last edited by a moderator:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
The only error I can find with your code is that the xlNo has a "1" (numeric one) instead of the letter l (L).
Rich (BB code):
Header:=x1No
'/sbe
Header:=xlNo
I am assuming you really don't have column headings in Row 43, otherwise the Header should be xlYes.

A more common way of laying it out would be below to have the Order expression paired with the Key expressions, although you normally would probably not hard code the end of the range ie 72 but find the last row in the code.
VBA Code:
Sub testSort()

    Dim rng As Range
    Dim ws As Worksheet
  
    Set ws = Worksheets("AllWeeks")
    Set rng = ws.Range("F43:I72")

    ws.Sort.SortFields.Clear
  
    With rng
        .Sort Key1:=ws.Range("H43"), Order1:=xlAscending, _
                Key2:=ws.Range("G43"), Order2:=xlAscending, _
                Key3:=ws.Range("I43"), Order3:=xlAscending, _
                Header:=xlNo
    End With

End Sub
 
Upvote 0
Solution
The only error I can find with your code is that the xlNo has a "1" (numeric one) instead of the letter l (L).
Rich (BB code):
Header:=x1No
'/sbe
Header:=xlNo
I am assuming you really don't have column headings in Row 43, otherwise the Header should be xlYes.

A more common way of laying it out would be below to have the Order expression paired with the Key expressions, although you normally would probably not hard code the end of the range ie 72 but find the last row in the code.
VBA Code:
Sub testSort()

    Dim rng As Range
    Dim ws As Worksheet
 
    Set ws = Worksheets("AllWeeks")
    Set rng = ws.Range("F43:I72")

    ws.Sort.SortFields.Clear
 
    With rng
        .Sort Key1:=ws.Range("H43"), Order1:=xlAscending, _
                Key2:=ws.Range("G43"), Order2:=xlAscending, _
                Key3:=ws.Range("I43"), Order3:=xlAscending, _
                Header:=xlNo
    End With

End Sub
Thank you very much for your quick reply... and for your eagle eyes!! Your recommended fix and your proposed use of the "common" code worked great.

Your advice about not hard coding the end range is noted and sometime in the near future I hope to understand how to accomplish that which you are suggesting. In this instance, I am dealing with a fixed range of data so I'm concerned about this range changing.

Thanks again for your help and guidance. It is much appreciated.
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,712
Members
452,939
Latest member
WCrawford

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