TOP

Sorting sheets in a book

Description

If the number of tabs in your book approaches several dozen, sooner or later there will be a desire to implement sorting sheets in a book , placing them in alphabetical order. The standard Excel tools don't allow you to do this, so let's write a simple macro that will do it.


VBA code for the macro

Open the menu Service - Macro - Editor Visual Basic (Tools - Macro - Visual Basic Editor) , insert module VBA (menu Insert - Module ) and copy this text there:

Sub SortSheets()
  'moonexcel.com.ua
  Dim I As Integer, J As Integer

    For I = 1 To Sheets.Count - 1
        For J = I + 1 To Sheets.Count
            If UCase(Sheets(I).Name) > UCase(Sheets(J).Name) Then
                Sheets(J).Move Before:=Sheets(I)
            End If
        Next J
    Next I
    
    MsgBox " Tabs were sorted from A to Z "
End Sub

This macro can now be run from the menu Service - Macro - Macros (Tools - Macro - Macros) , and it will quickly sort all the sheets in the current workbook.

Related Articles: