XL: VB Macro to Sort Custom List on First and Second KeysLast reviewed: February 3, 1998Article ID: Q136146 |
The information in this article applies to:
SUMMARYIn 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 INFORMATIONMicrosoft 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 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 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/refguide/default.aspThe 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
REFERENCESFor more information about sorting lists, click the Index tab in Microsoft Excel Help, type the following text
sorting data, listsand then double-click the selected text to go to the "Sort a list" topic.
|
Additional query words: 5.00 5.0a 5.0c 7.0 7.0a XL98 XL97 XL7 XL5 custom
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |