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!
Excel provides ways to reference the column or row number of a cell, but it doesn't provide a built-in way to reference a ...Discover More
If you are using a macro to process a number of worksheets, you may have a need to know if the worksheet is empty or not. ...Discover More
As you develop worksheets, it is not unusual to end up with two that are essentially the same. At some point you may want to ...Discover More
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.