XL: VB Macro to Sort Custom List on First and Second Keys
ID: Q136146
|
The information in this article applies to:
-
Microsoft Excel for Windows, versions 5.0, 5.0c
-
Microsoft Excel for the Macintosh, versions 5.0, 5.0a
-
Microsoft Excel for Windows NT, version 5.0
-
Microsoft Excel for Windows 95, versions 7.0, 7.0a
-
Microsoft Excel 97 for Windows
-
Microsoft Excel 98 Macintosh Edition
SUMMARY
In Microsoft Excel, you can select only one custom list to sort. However,
you can create a Microsoft Visual Basic for Applications macro or
procedure that uses multiple sort keys along with custom lists.
MORE INFORMATION
Microsoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
The following sample macro shows how you can perform a custom list sort on
multiple keys. The macro performs a custom sort on the second key and then
a normal sort on the first key.
Sub Second_Key_Custom_Sort()
' Selects the entire list.
Selection.CurrentRegion.Select
' Does the sort here with Custom List 1 which is Normal.
' It also assumes the list has headers.
' The sort is done on column A.
Selection.Sort Key1:=Range("A1"), Header:=xlYes, OrderCustom:=1
ActiveSheet.Range("a2").Select
' Line label.
Again:
' Loops until it reachs an empty cell.
Do Until IsEmpty(ActiveCell)
' Gets row number of the starting row.
StartRow = ActiveCell.Row
' Gets the value from the starting cell.
CellVal = ActiveCell.Value
' Starts a loop that will continue until it finds a different
' value.
Do Until ActiveCell.Value <> CellVal
ActiveCell.Offset(1, 0).Select
Loop
' Selects one cell up.
ActiveCell.Offset(-1, 0).Select
' Gets the ending address.
EndAdd = ActiveCell.Address
' Gets the ending row.
EndRow = ActiveCell.Row
' Select from the beginning row to the ending row.
' If you know the columns you want to sort, you can
' concatenate them in here.
ActiveSheet.Range(StartRow & ":" & EndRow).Select
' Does the sort for the selection.
' Assumes no headers.
' Bases Sort on the 4th item in custom list.
Selection.Sort Key1:=Range("b" & StartRow), _
Header:=xlNo, OrderCustom:=4
' Selects the last cell in that group.
ActiveSheet.Range(EndAdd).Select
' Starts on the next line with a new value.
ActiveCell.Offset(1, 0).Select
' Goes to the line label again.
GoTo Again
Loop
End Sub
To Create and Run This Macro
- In a new workbook, enter the macro code on a module sheet.
- In Sheet1, enter the following sample custom list:
A1: Number B1: Month
A2: 1 B2: Sep
A3: 2 B3: Nov
A4: 1 B4: Jun
A5: 1 B5: Jan
A6: 2 B6: Mar
A7: 1 B7: Dec
A8: 2 B8: May
A9: 2 B9: Feb
A10: 1 B10: Apr
- Select any cell in the list.
- Run the macro.
REFERENCES
For more information about sorting lists, click the Index tab in
Microsoft Excel Help, type the following text
sorting data, lists
and then double-click the selected text to go to the "Sort a list"
topic.
Additional query words:
7.0 XL98 XL97 XL7 XL5 custom list sorting user-defined
Keywords : kbprg kbdta kbdtacode PgmHowto KbVBA
Version : MACINTOSH:5.0,5.0a,98; WINDOWS:5.0,5.0c,7.0,7.0a,97; winnt:5.0
Platform : MACINTOSH WINDOWS winnt
Issue type : kbhowto