richh
Board Regular
- Joined
- Jun 24, 2007
- Messages
- 245
- Office Version
- 365
- 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
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