Ordering Worksheets Based on a Cell Value

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
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.