Sorting a range in a spreadsheet

u123935

New Member
Joined
Feb 28, 2005
Messages
15
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to sort a section in a sheet using two keys. The code looks good but isn't (see below). When stepping through the code, it halts whenever it arrives
at the first .SortFields. line with run time error '48' Named argument not found. Anyone help?

' sort Primary income
Set ws = Worksheets("Primary")
Worksheets("Primary").Activate
lowrow = Range("A" & Rows.Count).End(xlUp).Row
' Range(Cells(5, "A"), Cells(lowrow, "D")).Select
Stop
With ActiveSheet.Sort
.SortFields.Add Key:=Range("C5"), Order1:=xlAscending
.SortFields.Add Key:=Range("A5"), Order1:=xlAscending
.SetRange Range(.Cells(5, "A"), .Cells(lowrow, "D"))
.Header = xlNo
.OrderCustom = 1
.MatchCase = False
.Orientation = x.lTopToBottom
End With
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I would think its because you havent fully qualified your sort area
try this see if it does the job. worth noting you dont need to 'select' your range either

VBA Code:
Set ws = Worksheets("Primary")
lowrow = ws.cells(Rows.Count,1).End(xlUp).Row

ws.Sort.SortFields.Clear
  
  'add range for column with criteria
   ws.Sort.SortFields.Add Key:=ws.range(ws1.Cells(5, 3), ws.Cells(lowRow, 3)), _ 
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
  
   ws.Sort.SortFields.Add Key:=ws.range(ws1.Cells(5, 1), ws.Cells(lowRow, 1)), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
        xlSortNormal
    
    With ws.Sort
        .SetRange ws.Range(ws.Cells(5, "A"), ws.Cells(lowrow, "D")) 'this range is the whole sort area
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
 
Upvote 0
Another option
VBA Code:
Dim ws As Worksheet
Set ws = Worksheets("Primary")
lowrow = ws.Range("A" & Rows.count).End(xlUp).Row
ws.Range("A5:D" & lowrow).Sort ws.Range("C5"), xlAscending, ws.Range("A5"), , xlAscending, Header:=xlNo
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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