Multi-Key Sort on sheet

richh

Board Regular
Joined
Jun 24, 2007
Messages
245
Office Version
  1. 365
  2. 2016
Hi Excel Gurus!

I have a sheet I've been trying to sort on three columns. I've been able to sort it on one column, but when I attempt to pass variables to "Key1/Key2/Key3", it doesn't seem to wakt to work. I want to sort it based on last name then first name then ID (Columns B, C, and E, respectively), which are passed as "B2", "C2", "E2" in the function call.

Method 1 I've tried:

Public Function sortDB2(locDB As Worksheet, keyC As String, keyD As String, keyE As String) //passing sheet and the strings "B2", "C2", and "E2".

Dim lRow As Long

lRow = locDB.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).row //Finding the last row to use in the sort range

With locDB.Sort
.SortFields.Clear
.SortFields.Add(key:=Range(keyC), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal) _
.SortFields.Add(key:=Range(keyD), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal) _
.SortFields.Add(key:=Range(keyE), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal) _
.SetRange Range(Cells(1, 1), Cells(lRow, 22))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

'MsgBox "Sort complete.", vbInformation

End Function

Method 2 I've tried:


Public Function sortDB2(locDB As Worksheet, keyC As String, keyD As String, keyE As String)

Dim lRow As Long

lRow = locDB.Cells.Find(What:="*", SearchOrder:=xlRows, _
SearchDirection:=xlPrevious, LookIn:=xlValues).row

With locDB.Sort
.SortFields.Clear
.SortFields.Add key1:=Range(keyC), Order1:=xlAscending, _
key2:=Range(keyD), Order2:=xlAscending, _
key3:=Range(keyE), Order3:=xlAscending, _
SortOn:=xlSortOnValues, DataOption:=xlSortNormal
.SetRange Range(Cells(1, 1), Cells(lRow, 22))
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

'MsgBox "Sort complete.", vbInformation

End Function

If someone can give me a pointer or two as to why the keys aren't working out
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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