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

Sorry for what undoubtedly a dumb question. I have a large spreadsheet that
I'm trying to create a macro that will sort the data (12,000+ rows) using a
sort with 4 keys. The following code gets me 3 columns - but will not
support the addition of a "Key4".

When I do it manually it's a two stage process - first, sort on the entire
spreadsheet on the first 3 columns - next stage is to then manually page
through the spreadsheet and performing a "mini-sort" on columns 2, 3 and 4
whenever I see the value of column 1 has changed.

I'm thinking I'll need to sort on the column 1 - then loop through the
spreadsheet identifying to identify the first row in which a new value
appears in column one and continue on to identify the last row in which that
same value appears. Then execute a sort on a range defined by the first /
last row identified. Unfortunately, I'm brand new to this VB stuff and
simply don't know how to do this.

Can anybody point me in the right direction?

SpaceNorman

********
Sub NewSort()
Range("SortRange").Select
Selection.Sort Key1:=Range("W5"), Order1:=xlAscending, Key2:=Range("X5") _
, Order2:=xlAscending, Key3:=Range("Y5"), Order3:=xlAscending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Range("A1").Select
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 913
Default Sorting more than 3 columns

On Mon, 7 Apr 2008 14:04:01 -0700, Space Norman
wrote:

Sorry for what undoubtedly a dumb question. I have a large spreadsheet that
I'm trying to create a macro that will sort the data (12,000+ rows) using a
sort with 4 keys. The following code gets me 3 columns - but will not
support the addition of a "Key4".

When I do it manually it's a two stage process - first, sort on the entire
spreadsheet on the first 3 columns - next stage is to then manually page
through the spreadsheet and performing a "mini-sort" on columns 2, 3 and 4
whenever I see the value of column 1 has changed.

I'm thinking I'll need to sort on the column 1 - then loop through the
spreadsheet identifying to identify the first row in which a new value
appears in column one and continue on to identify the last row in which that
same value appears. Then execute a sort on a range defined by the first /
last row identified. Unfortunately, I'm brand new to this VB stuff and
simply don't know how to do this.

Can anybody point me in the right direction?

SpaceNorman

********
Sub NewSort()
Range("SortRange").Select
Selection.Sort Key1:=Range("W5"), Order1:=xlAscending, Key2:=Range("X5") _
, Order2:=xlAscending, Key3:=Range("Y5"), Order3:=xlAscending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Range("A1").Select
End Sub


Try this:

First sort on the least significant column.
Then sort on the other three columns.
The result will be what you expect - I guess.

Hope this helps / Lars-Åke
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default Sorting more than 3 columns

Yes, it's too bad we're limited to three columns, but usually that's
sufficient. What I do is create a "Sorter" column concatenating all four key
columns into it:

(Col-E) = CONCATENATE(A1,B1,C1,D1)

Make sure key columns are in the correct order. Copy down column "E" (or
whatever), and sort on the Sorter column. Hide it if you want.

"Space Norman" wrote:

Sorry for what undoubtedly a dumb question. I have a large spreadsheet that
I'm trying to create a macro that will sort the data (12,000+ rows) using a
sort with 4 keys. The following code gets me 3 columns - but will not
support the addition of a "Key4".

When I do it manually it's a two stage process - first, sort on the entire
spreadsheet on the first 3 columns - next stage is to then manually page
through the spreadsheet and performing a "mini-sort" on columns 2, 3 and 4
whenever I see the value of column 1 has changed.

I'm thinking I'll need to sort on the column 1 - then loop through the
spreadsheet identifying to identify the first row in which a new value
appears in column one and continue on to identify the last row in which that
same value appears. Then execute a sort on a range defined by the first /
last row identified. Unfortunately, I'm brand new to this VB stuff and
simply don't know how to do this.

Can anybody point me in the right direction?

SpaceNorman

********
Sub NewSort()
Range("SortRange").Select
Selection.Sort Key1:=Range("W5"), Order1:=xlAscending, Key2:=Range("X5") _
, Order2:=xlAscending, Key3:=Range("Y5"), Order3:=xlAscending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Range("A1").Select
End Sub


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 50
Default Sorting more than 3 columns

I am not sure what version of Excel you are using, but this seemed to work
for me using Excel 2007. This was a short sort range of 12 rows, but you
should be able to change the ending range to encompass what you are looking
for.

Sub sorttest()
'
' sorttest Macro
'

'
Columns("A:E").Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("A2:A12") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("B2:B12") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("C2:C12") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("D2:D12") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("Sheet1").Sort.SortField s.Add
Key:=Range("E2:E12") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:E12")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub

"Space Norman" wrote:

Sorry for what undoubtedly a dumb question. I have a large spreadsheet that
I'm trying to create a macro that will sort the data (12,000+ rows) using a
sort with 4 keys. The following code gets me 3 columns - but will not
support the addition of a "Key4".

When I do it manually it's a two stage process - first, sort on the entire
spreadsheet on the first 3 columns - next stage is to then manually page
through the spreadsheet and performing a "mini-sort" on columns 2, 3 and 4
whenever I see the value of column 1 has changed.

I'm thinking I'll need to sort on the column 1 - then loop through the
spreadsheet identifying to identify the first row in which a new value
appears in column one and continue on to identify the last row in which that
same value appears. Then execute a sort on a range defined by the first /
last row identified. Unfortunately, I'm brand new to this VB stuff and
simply don't know how to do this.

Can anybody point me in the right direction?

SpaceNorman

********
Sub NewSort()
Range("SortRange").Select
Selection.Sort Key1:=Range("W5"), Order1:=xlAscending, Key2:=Range("X5") _
, Order2:=xlAscending, Key3:=Range("Y5"), Order3:=xlAscending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Range("A1").Select
End Sub


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Sorting more than 3 columns

On Apr 7, 11:04 pm, Space Norman
wrote:
Sorry for what undoubtedly a dumb question. I have a large spreadsheet that
I'm trying to create a macro that will sort the data (12,000+ rows) using a
sort with 4 keys. The following code gets me 3 columns - but will not
support the addition of a "Key4".

When I do it manually it's a two stage process - first, sort on the entire
spreadsheet on the first 3 columns - next stage is to then manually page
through the spreadsheet and performing a "mini-sort" on columns 2, 3 and 4
whenever I see the value of column 1 has changed.

I'm thinking I'll need to sort on the column 1 - then loop through the
spreadsheet identifying to identify the first row in which a new value
appears in column one and continue on to identify the last row in which that
same value appears. Then execute a sort on a range defined by the first /
last row identified. Unfortunately, I'm brand new to this VB stuff and
simply don't know how to do this.

Can anybody point me in the right direction?

SpaceNorman

********
Sub NewSort()
Range("SortRange").Select
Selection.Sort Key1:=Range("W5"), Order1:=xlAscending, Key2:=Range("X5") _
, Order2:=xlAscending, Key3:=Range("Y5"), Order3:=xlAscending,
Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal, DataOption2:=xlSortTextAsNumbers,
DataOption3 _
:=xlSortNormal
Range("A1").Select
End Sub


declare SortRange
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
freezing columns labels while sorting columns Stan New Users to Excel 1 December 3rd 09 11:30 AM
sorting columns dean d. New Users to Excel 1 May 6th 08 05:21 PM
Sorting by Two Columns Luke Slotwinski Excel Worksheet Functions 7 November 3rd 06 07:17 AM
help with sorting text in columns to match other columns rkat Excel Discussion (Misc queries) 1 August 11th 06 03:42 AM
Regarding Sorting Columns Ben Excel Discussion (Misc queries) 2 April 4th 05 08:05 PM


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