Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Alphabetizing Worksheet Tabs.

Alphabetizing Worksheet Tabs

Written by Allen Wyatt (last updated September 17, 2022)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021


2

Pat wonders if there is a quick way to alphabetize the worksheets in her workbook. There is no built-in method to do this, but it is possible to use a macro to do the sorting for you. I'll present two approaches in this tip. The first provides a simple way to do the sorting.

Sub SortSheets1()
    Dim I As Integer
    Dim J As Integer

    For I = 1 To Sheets.Count - 1
        For J = I + 1 To Sheets.Count
            If UCase(Sheets(I).Name) > UCase(Sheets(J).Name) Then
                Sheets(J).Move Before:=Sheets(I)
            End If
        Next J
    Next I
End Sub

This macro sorts the worksheet tabs by name in ascending order. If you want to sort them in descending order, all you need to do is change the > in the center line to <.

The macro works great if you have a relatively low number of worksheets in your workbook. If, when you run the macro, you note that it takes a great deal of time to run, you may want to use a more efficient sorting algorithm in the macro. For instance, the following is a version that reads the names of all the worksheets into an array, sorts the array using the BubbleSort algorithm, and then does the actual arranging:

Sub SortSheets2()
    Dim I As Integer
    Dim sMySheets() As String
    Dim iNumSheets As Integer

    iNumSheets = Sheets.Count
    Redim sMySheets(1 To iNumSheets)

    For I = 1 To iNumSheets
        sMySheets(I) = Sheets(I).Name
    Next I

    BubbleSort sMySheets

    For I = 1 To iNumSheets
        Sheets(sMySheets(I)).Move Before:=Sheets(I)
    Next I
End Sub
Sub BubbleSort(sToSort() As String)
    Dim Lower As Integer
    Dim Upper As Integer
    Dim I As Integer
    Dim J As Integer
    Dim K As Integer
    Dim Temp As String

    Lower = LBound(sToSort)
    Upper = UBound(sToSort)
    For I = Lower To Upper - 1
        K = I
        For J = I + 1 To Upper
            If sToSort(K) > sToSort(J) Then
                K = J
            End If
        Next J
        If I <> K Then
            Temp = sToSort(I)
            sToSort(I) = sToSort(K)
            sToSort(K) = Temp
        End If
    Next I
End Sub

Anyone who has programmed for some time knows that BubbleSort is a good general-purpose sorting routine, but there are faster ones available. For instance, if you have quite a few worksheets, and they start out very disorganized, you may find that the QuickSort algorithm is more beneficial. All you would need to do to change the above to use QuickSort is add the QuickSort algorithm as a subroutine (you can find the algorithm in any good Visual Basic programming book) and then call the procedure from within the main SortSheets macro. (This means changing the line where BubbleSort is now called.)

There is another difference between this second macro and the first. The first macro does not pay attention to the case of the text used to name your worksheets. Thus, MyWorksheet would be viewed the same as MYWORKsheet. The second macro does pay attention to text case, and sorts accordingly. Of course, this is not a particularly big issue, since Excel doesn't pay attention to case in worksheet names, either.

Just a quick note: The BubbleSort routine shown above is general-purpose in nature. That means it will work for sorting any array, not just for sorting the names of worksheet tabs.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13440) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, Excel in Microsoft 365, and 2021. You can find a version of this tip for the older menu interface of Excel here: Alphabetizing Worksheet Tabs.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Identifying Unused Named Ranges

Named ranges can make it easier to refer to ranges of cells in an understandable way. If you want to delete named ranges ...

Discover More

Hiding a Hyperlink on a Printout

Hyperlinks can be real handy in a workbook, but you may not always want them visible when you send the workbook to the ...

Discover More

Adjusting Cell Margins for More White Space

Is the information in your cells too jammed up? Here are some ways you can add some white space around that information ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Jumping to Alphabetic Worksheets

Got a workbook with a lot of worksheets in it? Here are some handy ways to jump to the worksheet you want, alphabetically.

Discover More

Changing the Color of Worksheet Gridlines

Want the gridlines in your worksheet to be a different color? You aren't limited to stodgy black; Excel lets you make ...

Discover More

Unbreakable Formula References to Worksheets

Excel allows you, in your formulas, to include references to cells on other worksheets. Those references include the name ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2023-01-11 11:02:04

J. Woolley

Here's another version of the SortSheets2 macro, but is requires the SORT function in Excel 365 or 2021:

Sub SortSheets3()
    Dim I As Integer
    Dim sMySheets() As Variant 'not String
    Dim iNumSheets As Integer
    iNumSheets = Sheets.Count
    ReDim sMySheets(1 To iNumSheets)
    For I = 1 To iNumSheets
        sMySheets(I) = Sheets(I).Name
    Next I
    sMySheets = WorksheetFunction.Sort(sMySheets, , , True)
    For I = 1 To (iNumSheets - 1)
        If sMySheets(I) <> Sheets(I).Name Then _
            Sheets(sMySheets(I)).Move Before:=Sheets(I)
    Next I
End Sub


2023-01-11 10:32:08

J. Woolley

In the Tip's SortSheets2 macro, the last For loop
    For I = 1 To iNumSheets
        Sheets(sMySheets(I)).Move Before:=Sheets(I)
    Next I
can be replaced by
    For I = 1 To (iNumSheets - 1)
        If sMySheets(I) <> Sheets(I).Name Then _
            Sheets(sMySheets(I)).Move Before:=Sheets(I)
    Next I
for a more efficient result.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.