XL: VB Macro to Sort Custom List on First and Second Keys

Last reviewed: February 3, 1998
Article 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 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.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

  1. In a new workbook, enter the macro code on a module sheet.

  2. 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
    
    

  3. Select any cell in the list.

  4. 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: 5.00 5.0a 5.0c 7.0 7.0a XL98 XL97 XL7 XL5 custom
list sorting user-defined
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,5.0c,7.0,97; MACINTOSH:5.0,5.0a,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.