View Single Post
  #29   Report Post  
Posted to microsoft.public.excel.programming
Howard Howard is offline
external usenet poster
 
Posts: 536
Default Worksheet sorting code/technique advise


If you implement a methodology that tells your code what to do then no

problem in specifying sort order.



Example:

[SortCriteria] = "2,1,3:a|4,5:d"



..then split the string by the pipe delimiter to get a 2-element 1D

array. Then split each element by the colon delimiter, then split that

by the comma delimiter...



Dim vSortList, vCols, vSortOrder, v, n&, j&



vSortList = Split([SortCriteria], "|")

For n = LBound(vSortList) To UBound(vSortList)

vCols = Split(vSortList(n), ":")

If vCols(1) = "a" Then vSortOrder = xlAscending _

Else vSortOrder = xlDescending

For Each v In Split(vCols(0))

Columns(CLng(v)).Sort Key1:=Cells(1, CLng(v)), _

Order1:=xlAscending, Header:=xlNo, _

OrderCustom:=1, MatchCase:=False, _

Orientation:=xlTopToBottom, _

DataOption1:=xlSortNormal

Next 'v

Next 'n



..or the like. Personally, I prefer users to enter column labels

because they can just read as when entering col nums it's easy to make

counting mistakes. In this case use the +/- symbols for sort order.

So...



If vCols(1) = "+" Then vSortOrder = xlAscending _

Else vSortOrder = xlDescending


Okay, I'm getting most of this, although I am pretty good at screwing stuff up when putting the code to work.<g

I think I'll start with the "a" and the "d" for sort orders.

Where you say you forgot the delimiter and then show this:

For Each v In Split(vCols(0), ",")

Does the "|" pipe delimiter also need to be in the "woops I forgot..."
correction? Like maybe For Each v In Split(vCols(0), ",","|")

Or is it a default thing when used in this situation?

Howard