Excel VBA: Sort Worksheets Alphabetically
In this article
Yes, you can sort worksheets alphabetically in Excel with a short VBA macro, because Excel itself has no built-in tab-sorting command. The macro loops through the tab names, compares them, and moves each sheet into position. A minimal working version is a For loop that compares each sheet name to the one before it and calls Worksheets(i).Move Before:=Worksheets(j) until no swaps remain. The steps below build that macro, run it safely, and cover the cases where it fails.
What You'll Need
- Excel on Windows or Mac with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or Excel > Preferences > Ribbon & Toolbar on Mac).
- A workbook saved as
.xlsm(macro-enabled) or.xlsbif you want to keep the macro. A plain.xlsxfile discards VBA when saved. - A backup copy of the workbook. Sorting tabs changes sheet order, and that order is saved with the file.
- Macro security set to allow macros: Developer > Macro Security, or Trust Center settings. Labels differ slightly by version, device and region.
Step-by-Step
- Save a copy first. Use File > Save As and give the copy a new name. Sheet order is part of the file, so an unwanted sort is easy to undo only if you have a copy or have not saved yet.
- Open the VBA editor. Press
Alt+F11on Windows. On Mac, use Tools > Macro > Visual Basic Editor, orOption+F11depending on your keyboard setup. - Insert a module. In the editor, choose Insert > Module. A blank code window appears in the project tree under your workbook name.
- Paste the macro. Copy the code below into the module. It sorts every visible worksheet in the active workbook into ascending A-to-Z order by tab name.
Sub SortSheetsAlphabetically() Dim i As Long, j As Long Dim ws As Worksheet Application.ScreenUpdating = False For i = 1 To ActiveWorkbook.Worksheets.Count For j = i + 1 To ActiveWorkbook.Worksheets.Count If UCase(ActiveWorkbook.Worksheets(j).Name) < _ UCase(ActiveWorkbook.Worksheets(i).Name) Then ActiveWorkbook.Worksheets(j).Move Before:=ActiveWorkbook.Worksheets(i) End If Next j Next i Application.ScreenUpdating = True End Sub - Understand what the code does. The outer loop fixes one position at a time. The inner loop scans the sheets after it and moves any sheet whose name sorts earlier.
UCasemakes the comparison case-insensitive, so "data" and "Data" are treated the same. Without it, Excel's default text comparison is case-sensitive and lowercase names sort after uppercase ones. - Run it. Click inside the macro and press
F5, or close the editor and use Developer > Macros, selectSortSheetsAlphabetically, and click Run. The tabs reorder immediately. - Check the result. Look at the tab bar. Names should now read A, B, C. If the workbook has many tabs, use the tab navigation arrows at the bottom left to scroll through them.
- Save as macro-enabled. Use File > Save As and pick Excel Macro-Enabled Workbook (
.xlsm). If you keep the file as.xlsx, Excel warns that the macro will be removed. - Optional: sort descending. Change the
<in the comparison to>to reverse the order. Keep a copy of the original macro if you want both directions. - Optional: sort only a range of tabs. Replace the
1inFor i = 1 To ...with the first tab position you want to touch, and the count with the last position. This leaves tabs outside that range untouched, which matters when a cover sheet must stay first.
Common Mistakes
Forgetting chart sheets. The macro uses Worksheets, which ignores chart sheets. If your workbook contains chart sheets, they stay where they are while worksheet tabs move around them, so the final order looks wrong. Use Sheets instead of Worksheets if you want chart sheets included, but note that Sheets also covers other sheet types and the comparison still works on names.
Hidden sheets. Hidden worksheets are still members of the Worksheets collection, so they get moved too. That is usually fine, but if a hidden helper sheet must stay at the end, unhide it, note its name, and adjust the loop range or skip it with an If ... Visible test.
Case sensitivity. Dropping UCase produces an order that looks random to anyone expecting dictionary order, because uppercase letters sort before lowercase ones in a plain string comparison.
Numbers in names. Text comparison puts "Sheet10" before "Sheet2" because the character "1" comes before "2". If your tabs are numbered, zero-pad them (Sheet02, Sheet10) or accept the text order.
Running it on the wrong workbook. ActiveWorkbook refers to whichever file has focus. If you run the macro from the editor while another workbook is active, the wrong file gets sorted. Switch to the intended workbook first, or replace ActiveWorkbook with ThisWorkbook so the macro always targets the file that contains it.
Saving as .xlsx. The macro runs fine, then disappears on save. Use .xlsm.
Tips
- Turn off
ScreenUpdatingas shown; with dozens of tabs the screen flicker is distracting and slows the sort. - Add
Application.DisplayAlerts = Falsebefore the loop and set it back toTrueafter, but only if you are sure no sheet has a name conflict or protected structure. Alerts exist to warn you. - Sheet protection does not block moving tabs, but workbook structure protection does. Unprotect the workbook structure first (Review > Protect Workbook) if the macro errors.
- Store the macro in your Personal Macro Workbook if you sort tabs often, so it is available in every file. In the VBA editor, that workbook is listed as PERSONAL.XLSB.
- Assign the macro to a button on a sheet (Developer > Insert > Button) for one-click sorting.
- Keep a "before" copy of the tab order in a cell range if the workbook is shared, so you can restore it manually.
Troubleshooting
The macro does nothing. Check that the code is in a standard module, not a sheet module, and that macros are enabled. If the Developer tab shows the macro but Run is greyed out, macro security is blocking it.
Run-time error 1004. This usually means the workbook structure is protected or a sheet name conflicts with an existing one. Unprotect the workbook structure and re-run.
Only some tabs moved. Chart sheets, hidden sheets or a limited loop range explain this. Confirm which collection you used and what range the loops cover.
The order looks wrong for numbered tabs. That is text sorting, not a bug. Zero-pad the names or sort numerically with a custom comparison.
You sorted the wrong file. Close without saving, reopen the backup, and change ActiveWorkbook to ThisWorkbook before running again.
Excel is slow or unresponsive. With very large workbooks, the move operations take time. Let it finish; do not click the tabs while it runs.
Frequently Asked Questions
Can I sort tabs alphabetically without VBA?
Not directly. Excel has no built-in tab sort command. You can drag tabs manually, or right-click a tab and use Move or Copy to reposition sheets one at a time.
Does the macro sort hidden sheets?
Yes, hidden worksheets are included because they are still part of the Worksheets collection. Add a visibility check if you want to skip them.
Will it sort chart sheets too?
Only if you change Worksheets to Sheets. As written, chart sheets stay put.
Is the sort case-sensitive?
Not in the version above, because UCase normalises names before comparing. Remove it and the sort becomes case-sensitive.
Can I undo the sort?
Ctrl+Z does not undo sheet moves. Reopen the unsaved file or your backup, or drag the tabs back manually.
Does this work in Excel for Mac?
Yes, the VBA code is the same. Only the way you open the editor and the ribbon labels differ slightly.