VBA macros · Sheets
VBA macro to sort sheets alphabetically in Excel
Put the sheet tabs of a workbook in A to Z (or Z to A) order with one VBA macro. Works with any number of sheets, including chart sheets.
Excel has no button to sort sheet tabs. In a workbook with one sheet per customer, employee or branch, finding a tab quickly gets hard once there are more than a dozen. The macro below moves the sheets into alphabetical order, ignoring upper and lower case, and turns screen updating off so the tabs do not flicker while they move.
Option Explicit
Sub SortSheetsAlphabetically()
' Sorts the sheets of the active workbook A to Z by name
Dim wb As Workbook
Dim i As Long, j As Long
Dim n As Long
Set wb = ActiveWorkbook
If wb Is Nothing Then Exit Sub
If wb.ProtectStructure Then
MsgBox "The workbook structure is protected. Unprotect it first.", vbExclamation
Exit Sub
End If
n = wb.Sheets.Count
If n < 2 Then Exit Sub
On Error GoTo Fail
Application.ScreenUpdating = False
For i = 1 To n - 1
For j = i + 1 To n
' StrComp with vbTextCompare ignores upper/lower case
If StrComp(wb.Sheets(j).Name, wb.Sheets(i).Name, vbTextCompare) < 0 Then
wb.Sheets(j).Move Before:=wb.Sheets(i)
End If
Next j
Next i
Application.ScreenUpdating = True
Exit Sub
Fail:
Application.ScreenUpdating = True
MsgBox "Could not sort the sheets: " & Err.Description, vbExclamation
End SubHow to use it
- Open the workbook and press
Alt+F11(Mac:Opt+F11orFn+Opt+F11) to open the VBA editor. See The Developer tab and the VBA editor if you have never used it. - Choose Insert › Module. An empty code window opens on the right.
- Paste the whole code into that window. If the module already starts with
Option Explicit, do not paste that line twice. - Go back to Excel, press
Alt+F8(Mac:Opt+F8, or Tools › Macro › Macros), pickSortSheetsAlphabeticallyand click Run. - Save the file as an Excel Macro-Enabled Workbook (.xlsm). A normal .xlsx file throws the code away when you save it.
To have this macro ready in every workbook, keep it in your Personal Macro Workbook (PERSONAL.XLSB). If you do not have one yet, record any tiny macro with Store macro in: Personal Macro Workbook (see The macro recorder: record, read and clean up code), then paste this code into a module under VBAProject (PERSONAL.XLSB). The macro works on whichever workbook is active when you run it.
How it works
The macro uses a simple sorting idea: for every position i it looks at all sheets to the right (j). Whenever it finds a sheet whose name comes earlier in the alphabet than the sheet currently at position i, it moves that sheet in front of it with Move Before:=. After the inner loop has finished, position i holds the smallest name of all remaining sheets, so after the outer loop the whole workbook is in order.
StrComp(a, b, vbTextCompare) returns -1 when a comes first, 0 when they are equal and 1 when b comes first. vbTextCompare makes the comparison case-insensitive, so apple and Apple sort together. A plain < comparison would put every name in capitals before every name in lower case.
Moving sheets is fast, but each move redraws the tab bar. Application.ScreenUpdating = False stops that, and the Fail handler switches it back on even when something goes wrong, so Excel never stays frozen. See Error handling and debugging in VBA and Make your macros fast: speed optimization in VBA for both patterns.
Change it
For Z to A order, flip the comparison to > 0. To keep the first sheet (a contents or summary sheet) where it is, start the outer loop at 2: For i = 2 To n - 1. Then rebuild your index with VBA macro to create a table of contents with hyperlinks in Excel so the links match the new order.
Names that contain numbers sort as text: Sheet10 comes before Sheet2, and Week 9 after Week 10. If that bothers you, rename the sheets with leading zeros (Week 09) or sort by a number pulled from the name, for example Val(Mid$(wb.Sheets(j).Name, 6)) against the same expression for sheet i.
To sort by tab colour or by a value in cell A1 instead, compare wb.Sheets(j).Tab.Color or wb.Sheets(j).Range("A1").Value in the If line (the second one only works if every sheet is a worksheet).
What can go wrong
Ctrl+Z (Mac: Cmd+Z). Save the workbook, or make a copy with VBA macro to save a backup copy with a timestamp in Excel, before you run it on real data. Sorting does not change any data, but restoring a carefully arranged tab order by hand is tedious.If the macro stops on a hidden sheet, unhide all sheets first with VBA macro to unhide all sheets in Excel, sort, then hide them again. A workbook that is shared in the old Shared Workbook mode cannot move sheets at all; turn sharing off first. Formulas, defined names and hyperlinks that refer to other sheets do not break when sheets move, because they point to sheet names, not positions. Code that uses Sheets(1) by index will, however, now point to a different sheet.
Related: VBA macro to create a table of contents with hyperlinks in Excel, VBA macro to list all sheet names in Excel, For loops: For...Next and For Each.