ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sorting rows programmatically (https://www.excelbanter.com/excel-programming/275975-sorting-rows-programmatically.html)

Michael Monteiro

Sorting rows programmatically
 
I have a range that I want to sort on a specific column.
There is a Sort method that does just what I want with
one exception. I want to define a custom sort order that
is not simply ascending or descending. Here is an example
to show what I mean. Suppose I have a column called "Col
A" that I want to sort on. Let's say the column can have
values "A, B, or C", but I want to define the sort order
as follows: B, C, A. In other words, I always show Bs,
followed by Cs, followed by As. I thought about creating
a "data table" on another sheet to specify this sort
order, but I'm not sure if I can use the Sort method to
do this. Auy thoughts?

(Unsorted)
Col A
1 A
2 B
3 C

(Sorted using a custom sort order)
Col A
1 B
2 C
3 A

Tom Ogilvy

Sorting rows programmatically
 
You can create a custom sort order as a custom list (tools=Options =
customs lists). However, you can't specify

B, C, A

and have it sort

Andy
Bill
Charles

as

Bill
Charles
Andy

You would have to make a custom list

Bill, Charles, Andy

and then it would sort as you want.

The sort command has an option to specify the list to use, just as you can
do it manually using the options button in the sort dialog.

--
Regards,
Tom Ogilvy

"Michael Monteiro" wrote in message
...
I have a range that I want to sort on a specific column.
There is a Sort method that does just what I want with
one exception. I want to define a custom sort order that
is not simply ascending or descending. Here is an example
to show what I mean. Suppose I have a column called "Col
A" that I want to sort on. Let's say the column can have
values "A, B, or C", but I want to define the sort order
as follows: B, C, A. In other words, I always show Bs,
followed by Cs, followed by As. I thought about creating
a "data table" on another sheet to specify this sort
order, but I'm not sure if I can use the Sort method to
do this. Auy thoughts?

(Unsorted)
Col A
1 A
2 B
3 C

(Sorted using a custom sort order)
Col A
1 B
2 C
3 A





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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com