User Agents

Web browsers and everyday web tech, explained

How to Sort Excel Sheet Tabs Alphabetically

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Sorting sheet tabs alphabetically in Excel organizes your workbook so you can find sheets faster, especially when you have many tabs. This guide shows you how to do it manually using built-in Excel features—no VBA or macros required. You'll need Excel for Microsoft 365, Excel 2021, 2019, 2016, or earlier versions; the steps are nearly identical across these.

What You'll Need

  • An Excel workbook with at least two sheet tabs.
  • Access to the sheet tabs at the bottom of the Excel window.
  • Basic familiarity with right-clicking and using the ribbon.

Step-by-Step

  1. Open your workbook in Excel. Make sure you are in the worksheet view where the sheet tabs are visible at the bottom.
  2. Right-click any sheet tab (e.g., the first tab). A context menu appears.
  3. Select "View Code" from the context menu. This opens the Microsoft Visual Basic for Applications (VBA) editor. Don't worry—you won't need to write code manually; you'll paste a simple script.
  4. In the VBA editor, click on "Insert" in the top menu, then choose "Module". This creates a new module where you can paste the sorting code.
  5. Copy the following VBA code and paste it into the module window:
    Sub SortSheetsAlphabetically()
        Dim i As Integer, j As Integer
        For i = 1 To Sheets.Count
            For j = i + 1 To Sheets.Count
                If Sheets(j).Name < Sheets(i).Name Then
                    Sheets(j).Move Before:=Sheets(i)
                End If
            Next j
        Next i
    End Sub
  6. Press F5 (or go to Run > Run Sub/UserForm) to execute the macro. Your sheet tabs will immediately be sorted alphabetically (A to Z).
  7. Close the VBA editor by clicking the X in the top-right corner. Your workbook is now sorted.
  8. Save your workbook (Ctrl+S). If you plan to use this macro again, save as a macro-enabled workbook (.xlsm) to preserve the code.

Common Mistakes

  • Not enabling macros: If you get a message about macros being disabled, you may need to enable them via the Developer tab or the security warning bar. However, for a one-time sort, you can run the macro directly from the VBA editor without enabling macros globally.
  • Copying code with extra characters: Make sure you copy the exact code without line numbers or bullet points. Extra spaces or characters can cause errors.
  • Sorting only visible sheets: The code sorts all sheets, including hidden ones. If you only want visible sheets, you'd need a more complex script, but for most users sorting all is fine.
  • Forgetting to save as .xlsm: If you save as .xlsx, the macro will be lost, and you'll have to redo the process next time.

Tips

  • Use a keyboard shortcut: After you've added the macro, you can assign a shortcut key (e.g., Ctrl+Shift+S) by going to Developer > Macros, selecting the macro, and clicking Options.
  • Sort descending: To sort Z to A, change the comparison in the code from < to > (i.e., If Sheets(j).Name > Sheets(i).Name).
  • Case sensitivity: The sort is case-insensitive by default in Excel's VBA string comparison, so "sheet1" and "Sheet1" are treated as equal. If you need case-sensitive sorting, add Option Compare Binary at the top of the module.
  • Keep a backup: Before running any macro, make a backup copy of your workbook in case something goes wrong.
  • Use for large workbooks: This method is much faster than manually dragging tabs when you have dozens of sheets.

Troubleshooting

  • Macro doesn't run: If you press F5 and nothing happens, check that the cursor is inside the Sub...End Sub block. Also ensure the module is not empty.
  • Error "Compile error: Sub or Function not defined": This usually means you typed the code incorrectly. Delete the module content and paste again from the instructions.
  • Sheets not sorted correctly: If the sort seems random, check for leading spaces in sheet names (e.g., " Sheet1"). Spaces affect alphabetical order. You can remove them manually or use a Trim function in the code.
  • VBA editor won't open: If right-clicking a tab doesn't show "View Code", you might be in a protected view. Click "Enable Editing" on the yellow bar, then try again.
  • Macro security warning: If you see a message about macros being disabled, click "Enable Content" on the security warning bar, or go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select "Enable all macros" (not recommended for daily use).
  • Sorting changes chart or data references: Moving sheets with formulas that reference other sheets can cause temporary #REF! errors if the references are not absolute. However, this is rare; usually Excel updates references automatically. If you see errors, undo (Ctrl+Z) and re-sort after checking formulas.

Frequently Asked Questions

Can I sort sheet tabs without using VBA?

Yes, you can manually drag and drop each tab to the desired position, but this is tedious for many sheets. There's no built-in one-click sort feature in Excel's ribbon, so VBA is the most efficient method.

Will sorting tabs break my formulas or charts?

Generally, no. Excel automatically updates references to sheets when you move them, as long as you use standard references like =Sheet2!A1. However, if you have 3D references or named ranges that span sheets, double-check them after sorting.

How do I sort tabs in reverse alphabetical order?

Modify the VBA code by changing the less-than sign (<) to a greater-than sign (>) in the comparison line. That will sort from Z to A.

Can I sort only specific sheets, not all?

Yes, but it requires a more complex script. You could list the sheet names you want to sort in an array and then sort only those. For most users, sorting all is simpler.

Will the macro work in Excel for Mac?

Yes, VBA is supported in Excel for Mac. The steps are similar, but the VBA editor may look slightly different. The code itself works the same.

How do I remove the macro after sorting?

You can leave it in the workbook (it won't affect anything) or delete the module: in the VBA editor, right-click the module in the Project Explorer and choose "Remove Module". Then save.