Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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.
by Allen Wyatt
(last updated October 28, 2017)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12448) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Ordering Worksheets Based on a Cell Value.
Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!
If you've got a lot of worksheets in your workbook, you may want to display their tabs in to rows at the bottom of the ...Discover More
When you add a new worksheet to a workbook, it receives a meaningful name such as "Sheet4" or "Sheet17." If you want to ...Discover More
If you want someone to not be able to move from one worksheet to another in a workbook, you've got your work cut out for ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.