Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Excel VBA: Sort Sheets by Name (Step-by-Step)

· 5 min read · Home Tech

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

To sort sheets by name with Excel VBA, open the VBA editor (Alt+F11), insert a new module, paste a short macro that loops through the Sheets collection comparing names and swapping their order with the Move method, then run it. The macro below sorts every worksheet in the active workbook alphabetically, ascending or descending, without touching cell contents. Because VBA has no built-in "sort sheets" command, the trick is a simple bubble sort that rearranges the tab order one sheet at a time.

What You'll Need

  • Excel for Windows or Mac with macros enabled (the .xlsm file format if you want to save the macro).
  • A workbook with at least two worksheets whose names you want ordered.
  • Permission to run macros — some organisations block them via Trust Center settings.
  • Optional: a backup copy of the workbook, since any macro that reorders sheets changes the file.

Step-by-Step

  1. Save a backup. Press Ctrl+S, then use File > Save As to keep a copy before running any macro that rearranges your workbook.
  2. Open the VBA editor. Press Alt+F11 (on Mac, Fn+Alt+F11 or the Developer tab > Visual Basic). If you do not see the Developer tab, enable it via File > Options > Customize Ribbon.
  3. Insert a module. In the editor, choose Insert > Module. A blank code window appears.
  4. Paste the sorting macro. Copy the code below into the module. It uses a bubble sort: it compares adjacent sheet names and moves a sheet left whenever its name should come earlier alphabetically.
    Sub SortSheetsByName()
        Dim i As Long, j As Long
        Dim ws As Worksheet
        Dim ascending As Boolean
        ascending = True  ' set to False for Z to A
    
        For i = 1 To ThisWorkbook.Worksheets.Count - 1
            For j = 1 To ThisWorkbook.Worksheets.Count - i
                Dim name1 As String, name2 As String
                name1 = LCase(ThisWorkbook.Worksheets(j).Name)
                name2 = LCase(ThisWorkbook.Worksheets(j + 1).Name)
                If (ascending And name1 > name2) Or (Not ascending And name1 < name2) Then
                    ThisWorkbook.Worksheets(j + 1).Move Before:=ThisWorkbook.Worksheets(j)
                End If
            Next j
        Next i
    End Sub
    The LCase function makes the comparison case-insensitive, so "apple" and "Apple" are treated the same. Change ascending = True to False for reverse order.
  5. Run the macro. Press F5 while the cursor is inside the subroutine, or use Run > Run Sub/UserForm. The tabs reorder instantly.
  6. Save the workbook as .xlsm. If you want to keep the macro, use File > Save As and choose Excel Macro-Enabled Workbook. A standard .xlsx file discards VBA code.
  7. Verify the order. Check the tab strip. If a sheet is hidden, it is still included in the sort because the Worksheets collection counts hidden sheets; the Visible property is not checked in this version.

Common Mistakes

  • Using Sheets instead of Worksheets. The Sheets collection can include chart sheets and dialog sheets, which have names too. Sorting them together with worksheets can produce unexpected order. Use Worksheets if you only care about data tabs.
  • Forgetting case sensitivity. Without LCase or UCase, VBA compares text by ASCII value, so all uppercase names sort before lowercase ones. That looks wrong to most users.
  • Running the macro on the wrong workbook. ThisWorkbook refers to the workbook containing the code. If you paste the macro into Personal.xlsb, it sorts that file, not your data file. Use ActiveWorkbook if you want the macro to act on whatever workbook is open.
  • Expecting numbers to sort naturally. Sheet names like "Sheet1", "Sheet2", "Sheet10" sort as text: Sheet1, Sheet10, Sheet2. If you need natural numeric order, pad numbers with zeros (Sheet01, Sheet02) or write a custom comparison.
  • Leaving the workbook in .xlsx. The macro runs fine in the session but is lost on save. Always save as .xlsm.

Tips

  • To sort only visible sheets, add a check: If ThisWorkbook.Worksheets(j).Visible = xlSheetVisible And ThisWorkbook.Worksheets(j + 1).Visible = xlSheetVisible Then before the comparison.
  • If you want to sort by a custom list (for example, months), replace the string comparison with a lookup against an array of names.
  • Assign the macro to a button: Developer > Insert > Button (Form Control), then draw it on a sheet and pick SortSheetsByName from the list.
  • For very large workbooks (dozens of sheets), the bubble sort is still fast enough because it only moves tabs, not data. There is no need for a more complex algorithm.
  • Test on a copy first. Sheet order affects formulas that use 3D references like =SUM(Sheet1:Sheet3!A1), so reordering can change results.

Troubleshooting

  • "Macros are disabled" message. Check File > Options > Trust Center > Trust Center Settings > Macro Settings. Enable macros for the session, or move the file to a trusted location. Do not lower security settings permanently.
  • Compile error: Expected: end of statement. A line was pasted incorrectly, often because smart quotes replaced straight quotes. Retype the quote characters in the VBA editor.
  • Nothing happens when you press F5. The cursor may be outside the subroutine. Click inside the code block first, or use Run > Run Sub/UserForm and select SortSheetsByName.
  • Sheets are protected. Sheet protection does not block reordering, but workbook structure protection does. Unprotect via Review > Protect Workbook before running the macro, then re-protect afterwards.
  • Wrong workbook sorted. As noted above, switch ThisWorkbook to ActiveWorkbook, or make sure the intended file is active when you run the macro.
  • Order still looks wrong. Check for leading spaces in sheet names. A name like " Sheet1" sorts before "Sheet1" because the space character comes first. Trim names manually or add a Trim call in the comparison.

Frequently Asked Questions

Can I sort sheets by name without VBA?

Not directly. Excel has no ribbon command to reorder tabs alphabetically. You can drag tabs manually, or right-click a tab and use Move or Copy to reposition sheets one at a time. VBA is the only built-in way to automate it.

Does this macro sort hidden sheets too?

Yes. The Worksheets collection includes hidden sheets, so they are reordered along with visible ones. Add a Visible check if you want to skip them.

Will sorting sheets break my formulas?

Formulas that refer to specific sheet names (like =Sheet2!A1) are unaffected. Formulas using 3D ranges across a span of sheets, such as =SUM(Sheet1:Sheet3!A1), can change results because the range now covers different sheets. Review those before saving.

Can I sort sheets by name in Excel for Mac?

Yes. The VBA editor is available in Excel for Mac, though the shortcut differs (Fn+Alt+F11 on many keyboards). The same macro code works, with the same .xlsm save requirement.

How do I sort sheets by name in descending order?

Change the line ascending = True to ascending = False in the macro. The comparison flips and the tabs reorder from Z to A.

Why does Sheet10 come before Sheet2?

VBA compares names as text, so "Sheet10" precedes "Sheet2" because the character "1" comes before "2". Pad numbers with leading zeros or write a custom numeric comparison to fix it.