View Single Post
  #31   Report Post  
Posted to microsoft.public.excel.programming
GS[_2_] GS[_2_] is offline
external usenet poster
 
Posts: 3,514
Default Worksheet sorting code/technique advise

After some thought, you can simplify the sort order process by adopting
the convention that the left hand list is ascending, the right
descending. This also handles if there's only 1 or the other...

For both: "2,1,3:4,5"

For Ascending only: "2,1,3,4,5:"

For Descending only: ":2,1,3,4,5"

...and handle it like so...

Sub SortCols()
Dim bOrderBoth As Boolean
Dim vSortCriteria, vCols, vSortOrder

'Assume both sort orders
bOrderBoth = True
vSortCriteria = Split([SortCriteria], ":")
If LBound(vSortCriteria) = Empty Then _
bOrderBoth = False: vSortOrder = xlDescending: GoTo SortU
If UBound(vSortCriteria) = Empty Then _
bOrderBoth = False: vSortOrder = xlAscending: GoTo SortL

SortL:
If bOrderBoth Then vSortOrder = xlAscending
For Each v In Split(vSortCriteria(0), ",")
Columns(CLng(v)).Sort Key1:=Cells(1, CLng(v)), _
Order1:=vSortOrder, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next 'v
If Not bOrderBoth Then Exit Sub

SortU:
If bOrderBoth Then vSortOrder = xlDescending
For Each v In Split(vSortCriteria(1), ",")
Columns(CLng(v)).Sort Key1:=Cells(1, CLng(v)), _
Order1:=vSortOrder, Header:=xlNo, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
Next 'v
End Sub

*Note:* This depicts as if still using col index. If you use col labels
then there's no need for the CLng() function...

[SortCriteria] = "b,a,c,d,e:" --OR-- "b,a,c:d,e" --OR-- ":b,a,c,d,e"

...for Ascending, both, or descending sort order.

--
Garry

Free uenet access at http://www.eternal-september.org
Classic VB Users Regroup
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion