Home life · Updated
How to Organize Excel Tabs Alphabetically
Quick answer To organize Excel tabs alphabetically, right-click any sheet tab, choose View Code, paste a short VBA macro, and run it. This sorts all sheets in ascending order instantly.
What You’ll Need
- Microsoft Excel (desktop version, not the web app, for VBA)
- Your workbook open with the tabs you want to sort
- Basic comfort with copy-pasting code (no programming experience needed)
Step-by-Step
- Save a backup copy of your workbook before sorting. Use File > Save As to create a duplicate, so if anything goes wrong you can revert.
- Open the VBA editor by pressing
Alt + F11on your keyboard. This opens a separate window titled “Microsoft Visual Basic for Applications.” - Insert a new module. In the VBA editor, go to the menu bar and click Insert > Module. A blank white coding area appears.
- Paste the sorting code into that blank area. Copy the following exactly:
Sub SortSheetsAlphabetically() Dim i As Long, j As Long For i = 1 To Sheets.Count For j = i + 1 To Sheets.Count If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then Sheets(j).Move Before:=Sheets(i) End If Next j Next i End SubMake sure you include the entire block, starting with
Suband ending withEnd Sub. - Run the macro. Press
F5while your cursor is inside the code, or go to Run > Run Sub/UserForm. Excel will instantly sort all sheet tabs alphabetically (A to Z). - Save your workbook as a macro-enabled file. Go to File > Save As, choose "Excel Macro-Enabled Workbook (*.xlsm)" from the file type dropdown, and save. If you keep it as .xlsx, the macro will be lost next time you open the file.
Common Mistakes
- Forgetting to enable macros: When you open the file later, Excel may block the macro. Use File > Options > Trust Center > Trust Center Settings > Macro Settings, then choose "Enable all macros" (temporarily) or "Disable VBA macros with notification" and click "Enable Content" when prompted.
- Typing the code manually: A single typo (like a missing quote or wrong letter) breaks the macro. Always copy-paste the code from a reliable source.
- Running the macro on the wrong workbook: The code sorts all sheets in the workbook that is active when you press F5. Make sure the correct file is in front of you.
- Not saving as .xlsm: If you save as .xlsx, Excel strips out the macro, and you'll have to redo it next time.
Tips
- Sort descending (Z to A): Change the
<in the code to>(greater than) to sort reverse alphabetically. - Keep certain tabs in place: If you want specific sheets (like a dashboard) to stay first, rename them with a prefix like "00_" or "1_" so they sort to the top naturally.
- Use the manual method for small workbooks: If you only have 5-10 tabs, you can drag them yourself. Click a tab, hold the mouse button, and drag left or right to reposition. This is faster than setting up VBA for a one-time sort.
- Make the macro reusable: Save the macro in your Personal Macro Workbook (during recording, choose "Personal Macro Workbook" as the destination). Then it's available in all your Excel files.
Troubleshooting
- Nothing happens when I press F5: Check that your cursor is inside the code window. If the code is not selected, Excel may run the wrong macro. Click anywhere in the code block first.
- Error "Compile error: Sub or Function not defined": This usually means you typed the code wrong or missed a line. Delete the code and paste it again from the instructions.
- Sheets are sorted but in a weird order: Excel sorts case-insensitively by default (the code uses UCase). If you have numbers in sheet names (like "Sheet10" and "Sheet2"), they sort as text, so "Sheet10" comes before "Sheet2". To fix, rename sheets with leading zeros (e.g., "Sheet02") or use a more advanced sorting code.
- Macro is blocked with a security warning: Click "Enable Content" in the yellow bar when you open the file. If it doesn't appear, go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" (then revert after you're done).
- VBA editor won't open: Pressing Alt+F11 does nothing? Make sure you're in the desktop Excel app, not the browser version. In Excel for the web, VBA is not supported.
Frequently Asked Questions
Can I sort Excel tabs alphabetically without using VBA?
Yes, but only manually. Drag each tab to its correct position. There's no built-in one-click sort for sheet tabs in standard Excel menus.
Will sorting tabs affect my formulas or data?
No. Sorting tabs only reorders the sheets themselves. Formulas that reference other sheets (like =Sheet2!A1) still work, but note that the references update automatically to the new sheet names if you rename them, not if you just reorder.
Can I undo the sort after running the macro?
Yes, immediately press Ctrl+Z after running the macro to undo the reorder. If you've done other actions since, you may not be able to undo; that's why the backup step is important.
Does this work in Excel for Mac?
Yes, the same VBA code works in Excel for Mac. To open the editor, press Option+F11 or go to Tools > Macro > Visual Basic Editor.
What if I want to sort only some tabs, not all?
Modify the code to specify which sheets to include. For example, you can replace Sheets.Count with a number, or use a condition like If Sheets(j).Name Like "Data*" to only sort sheets that start with "Data".
Is there a way to sort tabs automatically every time I open the file?
Yes, you can add a line to the code that runs automatically on workbook open. In the VBA editor, double-click "ThisWorkbook" in the left panel and paste: Private Sub Workbook_Open(). This will sort the tabs every time the file is opened.
SortSheetsAlphabetically
End Sub
Related
- How to Stretch Watercolour Paper: Step-by-Step
- How to Sort Numbers in Excel: Step-by-Step
- How to Alphabetize in Excel: Sort A-Z in Minutes
Sources: Microsoft Support. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.