Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Ordering Worksheets Based on a Cell Value.
by Allen Wyatt
(last updated June 23, 2018)
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 examine the key cell in each subsequent worksheet to see how it compares. 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 (2890) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Ordering Worksheets Based on a Cell Value.
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!
When switching from one worksheet to another, you might want to view the same portion of the new worksheet that you were ...Discover More
Excel makes copying worksheets (duplicating them) rather easy. However, you may want a worksheet copy that differs from ...Discover More
Copy a formula from one place to another and Excel helpfully adjusts the cell references within the formula. That is, it ...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 menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.