Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Sorting columns of data

Hi

I am trying to sort data like this

Name Field1 field2 field3 field4 field5
Joh 3 10 6 2 1
Bill 4 8 3 1 2
Fred 4 9 3 2 1
John 4 9 5 3 2
Adam 4 9 3 2 1

The end result should be
John 4 9 5 3 2
Adam 4 9 3 2 1
Fred 4 9 3 2 1
Bill 4 8 3 1 2
Joh 3 10 6 2 1

I want the field1 data to be sorted first then field2 then field3,
field4 then the name.

I tried this but get an error

Selection.Sort Key1:=Range("b45"), Order1:=xlDescending,
Key2:=Range("c45"), Order2:=xlDescending, Key3:=Range("d45"),
Order3:=xlDescending, Key4:=Range("e45"), Order4:=xlDescending,
Key5:=Range("a45"), Order5:=xlAscending, Header:=xlNo, OrderCustom:=1,
MatchCase:=False, Orientation:=xlTopToBottom,
DataOption1:=xlSortTextAsNumbers

Using excel 2010.
Can anyone help?

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 19
Default Sorting columns of data


A little more searching and I found some code I was able to modify to work.

Dim rng As Range
Set rng = Range("a45:f45", Selection.End(xlDown))


Dim srt As Sort

Dim sht As Worksheet
Set sht = ActiveSheet

Set srt = sht.Sort

srt.SortFields.Clear
srt.SortFields.Add Key:=Range("B45"), _
SortOn:=xlSortOnValues, Order:=xlDescending
srt.SortFields.Add Key:=Range("C45"), _
SortOn:=xlSortOnValues, Order:=xlDescending
srt.SortFields.Add Key:=Range("D45"), _
SortOn:=xlSortOnValues, Order:=xlDescending
srt.SortFields.Add Key:=Range("E45"), _
SortOn:=xlSortOnValues, Order:=xlDescending
srt.SortFields.Add Key:=Range("A45"), _
SortOn:=xlSortOnValues, Order:=xlAscending

' Set the sort range:
srt.SetRange rng
srt.Header = xlNo
srt.MatchCase = True
' Apply the sort:
srt.Apply

Original code here

http://msdn.microsoft.com/en-us/library/hh128804.aspx


On 25/01/2012 11:59 PM, XR8 Sprintless wrote:
Hi

I am trying to sort data like this

Name Field1 field2 field3 field4 field5
Joh 3 10 6 2 1
Bill 4 8 3 1 2
Fred 4 9 3 2 1
John 4 9 5 3 2
Adam 4 9 3 2 1

The end result should be
John 4 9 5 3 2
Adam 4 9 3 2 1
Fred 4 9 3 2 1
Bill 4 8 3 1 2
Joh 3 10 6 2 1

I want the field1 data to be sorted first then field2 then field3,
field4 then the name.

I tried this but get an error

Selection.Sort Key1:=Range("b45"), Order1:=xlDescending,
Key2:=Range("c45"), Order2:=xlDescending, Key3:=Range("d45"),
Order3:=xlDescending, Key4:=Range("e45"), Order4:=xlDescending,
Key5:=Range("a45"), Order5:=xlAscending, Header:=xlNo, OrderCustom:=1,
MatchCase:=False, Orientation:=xlTopToBottom,
DataOption1:=xlSortTextAsNumbers

Using excel 2010.
Can anyone help?

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
sorting data in columns Dawna Excel Programming 6 November 8th 09 02:47 PM
Sorting data in columns alan.holmes Excel Worksheet Functions 2 August 24th 08 07:40 PM
Sorting Data into columns without replacing the columns with data Sandaime New Users to Excel 2 October 18th 07 01:35 PM
sorting data in columns Spiderman Excel Discussion (Misc queries) 2 October 6th 05 09:45 PM
Sorting two columns of data Nick Holway Excel Programming 1 October 16th 03 07:53 PM


All times are GMT +1. The time now is 09:08 PM.

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

About Us

"It's about Microsoft Excel"