Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Sorting with variable number of sort fields

Hi

Anyone come up with an efficient way of sorting data based on variable
number of sort fields at runtime?

For example, if end-user wants to sort data on column 1, code is
straighttforward:
myRange.Sort key1:=.Cells(1,1)

However, if end-user can sort on upto 2 columns, say column x, and y, code
gets clunky:
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)

Now if end-user can sort on upto 3 columns, say x, y, z the code gets even
more clunky.
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)
else if (user specified columnx and y and z) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y), key3=Cells(1,z)

Is there a better solution?
Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Sorting with variable number of sort fields

Here is a possible work around:

Sub Macro1()
Dim v1 As Variant, v2 As Variant
Dim v3 As Variant
v1 = ""
v2 = ""
v3 = ""

Set v1 = Range("C1")

Range("A1:F28").Sort Key1:=v1, Order1:=xlAscending, _
Key2:=v2, Order2:=xlAscending, _
Key3:=v3, Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
End Sub

Leave V2 and V3 or just V3 as equal to "" if you don't want to sort on them.
Otherwise set them to a range.

--
Regards,
Tom Ogilvy

"BW" wrote in message
...
Hi

Anyone come up with an efficient way of sorting data based on variable
number of sort fields at runtime?

For example, if end-user wants to sort data on column 1, code is
straighttforward:
myRange.Sort key1:=.Cells(1,1)

However, if end-user can sort on upto 2 columns, say column x, and y, code
gets clunky:
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)

Now if end-user can sort on upto 3 columns, say x, y, z the code gets

even
more clunky.
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)
else if (user specified columnx and y and z) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y), key3=Cells(1,z)

Is there a better solution?
Thanks



  #3   Report Post  
Posted to microsoft.public.excel.programming
BW BW is offline
external usenet poster
 
Posts: 49
Default Sorting with variable number of sort fields

Thanks Tom, that works great!

"Tom Ogilvy" wrote:

Here is a possible work around:

Sub Macro1()
Dim v1 As Variant, v2 As Variant
Dim v3 As Variant
v1 = ""
v2 = ""
v3 = ""

Set v1 = Range("C1")

Range("A1:F28").Sort Key1:=v1, Order1:=xlAscending, _
Key2:=v2, Order2:=xlAscending, _
Key3:=v3, Order3:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, _
MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, _
DataOption2:=xlSortNormal
End Sub

Leave V2 and V3 or just V3 as equal to "" if you don't want to sort on them.
Otherwise set them to a range.

--
Regards,
Tom Ogilvy

"BW" wrote in message
...
Hi

Anyone come up with an efficient way of sorting data based on variable
number of sort fields at runtime?

For example, if end-user wants to sort data on column 1, code is
straighttforward:
myRange.Sort key1:=.Cells(1,1)

However, if end-user can sort on upto 2 columns, say column x, and y, code
gets clunky:
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)

Now if end-user can sort on upto 3 columns, say x, y, z the code gets

even
more clunky.
if (user specified column x only) then
myRange.Sort key1:=.Cells(1,x)
else if (user specified column x and y) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y)
else if (user specified columnx and y and z) then
myRange.Sort key1:=Cells(1,x), key2:=Cells(1,y), key3=Cells(1,z)

Is there a better solution?
Thanks




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding more sort fields to the SORT Dialog Box Doug V. Excel Discussion (Misc queries) 3 October 6th 08 07:30 PM
number of fields in the row fields in pivot table UT Excel Discussion (Misc queries) 0 April 13th 06 01:17 AM
SUM of a variable range of fields Nicolas Excel Worksheet Functions 2 March 31st 06 12:09 PM
Need pie chart with number of fields instead of info in fields Lloyd Pratt Charts and Charting in Excel 1 September 21st 05 11:56 PM
Sorting by more than 3 Fields in VBA Kiko Excel Programming 3 December 8th 04 06:39 PM


All times are GMT +1. The time now is 05:12 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"