Return to VBA Code Examples

VBA – Sort Sheets Alphabetically

Written by

Editorial Team

Reviewed by

Steve Rynearson

Last updated on July 19, 2021



The following routine will sort the sheets in a workbook alphabetically. The flag “Sort_Mode_Descending” can be switched between descending and ascending as required. The routine is case INSENSITIVE.

Sub Sort_Sheets()
Dim Sort_Mode_Descending As Boolean
Dim No_of_Sheets As Integer
Dim Outer_Loop As Integer
Dim Inner_Loop As Integer
No_of_Sheets = Sheets.Count
'Change Flag As appropriate
Sort_Mode_Descending = False
For Outer_Loop = 1 To No_of_Sheets
        For Inner_Loop = 1 To Outer_Loop
               If Sort_Mode_Descending = True Then
                If UCase(Sheets(Outer_Loop).Name) > UCase(Sheets(Inner_Loop).Name) Then
                                Sheets(Outer_Loop).Move Before:=Sheets(Inner_Loop)
                End If
           End If
          If Sort_Mode_Descending = False Then
            If UCase(Sheets(Outer_Loop).Name) < UCase(Sheets(Inner_Loop).Name) Then
                        Sheets(Outer_Loop).Move Before:=Sheets(Inner_Loop)
            End If
          End If

        Next Inner_Loop
            Next Outer_Loop

To download the .XLSM file from this article, click here.

VBA Coding Made Easy

Stop searching for VBA code online. Learn more about AutoMacro - A VBA Code Builder that allows beginners to code procedures from scratch with minimal coding knowledge and with many time-saving features for all users!

alt text

Learn More!


<<Return to VBA Examples

vba-free-addin

VBA Code Examples Add-in

Easily access all of the code examples found on our site.

Simply navigate to the menu, click, and the code will be inserted directly into your module. .xlam add-in.

(No installation required!)

Free Download

Return to VBA Code Examples