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: Ordering Worksheets Based on a Cell Value.

Ordering Worksheets Based on a Cell Value

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


5

Other issues of ExcelTips have provided ways that you can sort the worksheets in your workbook based on the worksheet name. What if you want to sort the worksheets based on a value in a given cell of each worksheet, however? For instance, you may have a series of worksheets that share the same general layout, and you want the worksheets ordered based on the value in cell H7 of each worksheet.

The only way to handle this is with a macro. The macro needs to step through each worksheet in the workbook, and then check the key cell in each subsequent worksheet to see how it compares to the same cell in other worksheets. If the cell value is less than the current worksheet, then the worksheet that contains the lesser value can be moved.

Sub SortWksByCell()
    Dim i As Integer
    Dim j As Integer

    For i = 1 To Worksheets.Count
        For j = i To Worksheets.Count
            If UCase(Worksheets(j).Range("H7")) < _
              UCase(Worksheets(i).Range("H7")) Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
        Next
    Next
End Sub

Note the use of the Move method, which does the actual movement of the worksheets. The names of the worksheets don't matter, only their positioning based on the value in cell H7 of each worksheet.

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 (12448) 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: Ordering Worksheets Based on a Cell Value.

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

Customizing Quick Access Toolbar Icons

The standard way to customize Excel is to add tools to the Quick Access Toolbar. The program provides only a limited ...

Discover More

Turning Off Track Changes Change Bars

Word includes a feature that allows you to track changes made to a document. One of the ways in which Word marks your ...

Discover More

Creating Dependent Drop-Lists

Drop-down lists are handy in an Excel worksheet, and you they can be even more handy if a selection in one drop-down ...

Discover More

Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!

More ExcelTips (ribbon)

Undeleting a Worksheet

Have you ever deleted a worksheet by mistake? Once you get over the sick feeling in your stomach, you will start casting ...

Discover More

Copying a Single Cell to Multiple Worksheets

If you need to copy a cell from one worksheet to a bunch of other worksheets, the work can quickly get tedious. Make the ...

Discover More

Properties for Worksheets

Excel keeps a full set of properties related to workbooks. When it comes to worksheets, however, there is very little ...

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 eight less than 8?

2023-01-11 12:17:06

J. Woolley

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

Sub SortWksByCell2()
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim V() As Variant 'not String
    k = Worksheets.Count
    ReDim V(1 To k, 1 To 2)
    For i = 1 To k
        V(i, 1) = UCase(Worksheets(i).Range("H7"))
        V(i, 2) = Worksheets(i).Name
    Next i
    V = WorksheetFunction.Sort(V)
    For i = 1 To (k - 1)
        If V(i, 2) <> Worksheets(i).Name Then _
            Worksheets(V(i, 2)).Move Before:=Worksheets(i)
    Next i
End Sub


2023-01-10 10:04:03

J. Woolley

@Philip
The sorting is based on the value of cell H7 in each worksheet. Chart sheets do not have a cell H7.
On the other hand, the Tip mentioned by Ron S. below sorts the Sheets collection, not Worksheets, despite limiting its discussion to worksheets. In that case, all chart sheets and worksheets are sorted by Name. Also, it uses the more efficient For loops described in my comment below.


2023-01-10 00:56:38

Philip

Both the solution proposed in the tip as the solution offered by J. Woolley only work on "worksheets". If your workbook also contains chart sheets, those won't be sorted (as they are not part of the Worksheets collection).

Wouldn't it be better to do this working with Sheets collection rather than Worksheets collection?


2023-01-08 09:48:10

J. Woolley

The Tip's macro implements a Bubble Sort; therefore, these For statements
    For i = 1 To Worksheets.Count
        For j = i To Worksheets.Count
can be replaced by
    Dim k As Integer
    k = Worksheets.Count
    For i = 1 To (k - 1)
        For j = (i + 1) To k
for a more efficient result. The first For skips the last worksheet and the second For skips the first For's worksheet.
Notice Worksheets(i) refers to a worksheet by location, not by Name, and the Name at a given location is likely to change during the sort.


2023-01-07 08:51:44

Ron S

Alphabetizing Worksheet Tabs
https://excelribbon.tips.net/T013440_Alphabetizing_Worksheet_Tabs.html


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.