#1   Report Post  
Member
 
Posts: 40
Default Sort Order

So, I'm trying to sort a specific way. Here is what I have.

Cells.Sort Key1:=Range("C:C"), Order1:=x1Descending

Unfortunately, there are at least a dozen variables and there are certain ones I want at the top. Basically, i want to say the order it goes in - not ascending or descending. Is there a way to specifiy the order?

Thanks,

J
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Sort Order

On Wed, 20 Mar 2013 12:08:01 +0000, JonathanK1 wrote:


So, I'm trying to sort a specific way. Here is what I have.

Cells.Sort Key1:=Range("C:C"), Order1:=x1Descending

Unfortunately, there are at least a dozen variables and there are
certain ones I want at the top. Basically, i want to say the order it
goes in - not ascending or descending. Is there a way to specifiy the
order?

Thanks,

J


You can use a custom list. In Excel 2007 or later:

Here's a snippet I use to sort using a custom list. In this case I am sorting horizontally to get columns in a desired order, but you can obviously modify it. I don't recall the method for earlier versions of Excel -- probably you need to explicitly add a custom list.

In my example, aCL is a one dimensional array that contains the items in sorted order, converted into a comma-separted list using the Join function, but CustomOrder can actually be any comma-separated list. (e.g: CustomOrder:= "ItemA","ItemC","ItemB","xxx" )

=====================
....
Set r = .UsedRange
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=r.Rows(1), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
CustomOrder:=Join(aCL, ",")
With .Sort
.SetRange r
.Header = xlYes
.Orientation = xlLeftToRight
.Apply
End With
.......
=====================================
  #3   Report Post  
Member
 
Posts: 40
Default

Quote:
Originally Posted by Ron Rosenfeld[_2_] View Post
On Wed, 20 Mar 2013 12:08:01 +0000, JonathanK1 wrote:


So, I'm trying to sort a specific way. Here is what I have.

Cells.Sort Key1:=Range("C:C"), Order1:=x1Descending

Unfortunately, there are at least a dozen variables and there are
certain ones I want at the top. Basically, i want to say the order it
goes in - not ascending or descending. Is there a way to specifiy the
order?

Thanks,

J


You can use a custom list. In Excel 2007 or later:

Here's a snippet I use to sort using a custom list. In this case I am sorting horizontally to get columns in a desired order, but you can obviously modify it. I don't recall the method for earlier versions of Excel -- probably you need to explicitly add a custom list.

In my example, aCL is a one dimensional array that contains the items in sorted order, converted into a comma-separted list using the Join function, but CustomOrder can actually be any comma-separated list. (e.g: CustomOrder:= "ItemA","ItemC","ItemB","xxx" )

=====================
....
Set r = .UsedRange
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=r.Rows(1), _
SortOn:=xlSortOnValues, Order:=xlAscending, _
CustomOrder:=Join(aCL, ",")
With .Sort
.SetRange r
.Header = xlYes
.Orientation = xlLeftToRight
.Apply
End With
.......
=====================================
Then for my needs, shouldn't this work?

Cells.Sort Key1:=Range("C:C"), Order1:=xlDescending, CustomOrder:="67,08,47,41,25,03,7,6,23"

It doesn't though, for whatever reasons.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,045
Default Sort Order

On Thu, 21 Mar 2013 11:45:29 +0000, JonathanK1 wrote:


Then for my needs, shouldn't this work?

Cells.Sort Key1:=Range("C:C"), Order1:=xlDescending,
CustomOrder:="67,08,47,41,25,03,7,6,23"

It doesn't though, for whatever reasons.


Works for me. Those values are at the top, other values sort below per excel sort rules.
Probably something about your data. If your customorder keys are a part of the string to be sorted, you may need to break them out into a helper column, and sort on that column. Don't forget to format as text so as to retain the leading zero's
  #5   Report Post  
Member
 
Posts: 40
Default

Quote:
Originally Posted by Ron Rosenfeld[_2_] View Post
On Thu, 21 Mar 2013 11:45:29 +0000, JonathanK1 wrote:


Then for my needs, shouldn't this work?

Cells.Sort Key1:=Range("C:C"), Order1:=xlDescending,
CustomOrder:="67,08,47,41,25,03,7,6,23"

It doesn't though, for whatever reasons.


Works for me. Those values are at the top, other values sort below per excel sort rules.
Probably something about your data. If your customorder keys are a part of the string to be sorted, you may need to break them out into a helper column, and sort on that column. Don't forget to format as text so as to retain the leading zero's
Hmm...not entirely sure what you mean. I don't know anything about "helper columns" etc. Here is what I have:

Cells.Sort Key1:=Range("C:C"), Order1:=xlDescending,_
CustomOrder:="67,08,47,25,03,07,23"
Key2:=Range("Q:Q"), Order2:=xlDescending, Header:=xlYes
End Sub


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
Retaining Sort order in the Sort Dialog box CBittinger Excel Discussion (Misc queries) 2 January 9th 08 05:01 PM
Sort sheet based on particuilar sort order Also Excel Worksheet Functions 4 January 3rd 08 09:31 AM
Sort Order? Ken Excel Discussion (Misc queries) 1 September 10th 07 04:34 PM
Pls. reply Sort Data and copy to next coulmn when sort order chang shital shah Excel Programming 1 August 19th 05 02:51 PM
sort order DGNVSPORTS Excel Discussion (Misc queries) 3 June 7th 05 07:19 PM


All times are GMT +1. The time now is 07:01 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"