Article ID: 136146
Article Last Modified on 10/11/2006
APPLIES TO
- Microsoft Excel 5.0c
- Microsoft Excel 5.0 for Macintosh
- Microsoft Excel 5.0a for Macintosh
- Microsoft Excel 5.0 Standard Edition
- Microsoft Excel 95a
- Microsoft Excel 97 Standard Edition
- Microsoft Excel 95 Standard Edition
- Microsoft Excel 5.0 Standard Edition
- Microsoft Excel 98 for Macintosh
This article was previously published under Q136146
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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. 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 XL
Keywords: kbhowto kbprogramming kbdtacode KB136146