
Tips.Net > ExcelTips Home > Worksheets > Ordering Worksheets Based on a Cell Value
Summary: Macros that sort your worksheets based on their name have been around for quite some time. You may not want to sort the worksheets based on name, but on the value in a cell on the worksheets. This tip provides a macro that provides just this type of sorting capability. (This tip works with Microsoft Excel 97, Excel 2000, Excel 2002, and Excel 2003.)
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 compare 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.
Tip #2890 applies to Microsoft Excel versions: 97 2000 2002 2003
Save Time and Money! Many people need to keep track of employee time, but don't know where to start when it comes to creating a spreadsheet. Here's a way to save time, effort, and money with ready-to-use timesheet templates.
Check out Timesheet Templates today!
It doesn't matter if you are a beginner or expert, the ExcelTips archives are the fastest way to improve your productivity. (more information...)
Ask an Excel Question
Make a Comment
ExcelTips FAQ
ExcelTips Premium
Beauty Tips
Bugs and Pests Tips
Car Tips
Cleaning Tips
College Tips
Cooking Tips
Excel2007 Tips
ExcelTips
Family Tips
Gardening Tips
Health Tips
Home Tips
Money Tips
Organizing Tips
Pet Tips
Word2007 Tips
WordTips
Advertise on the
ExcelTips Site