Sub SortSheets()
' This routine sorts the sheets of the
' active workbook in ascending order.
Dim SheetNames() As String
Dim SheetHidden() As Boolean
Dim i As Integer
Dim SheetCount As Integer
Dim VisibleWins As Integer
Dim Item As Object
Dim OldActive As Object
If ActiveWorkbook Is Nothing Then Exit Sub 'No active Workbook
SheetCount = ActiveWorkbook.Sheets.Count
' Check for protected workbook structure
If ActiveWorkbook.ProtectStructure Then
MsgBox ActiveWorkbook.Name & " is protected .", vbCritical, "Cannot Sort Sheets."
Exit Sub
End If
' Disable Ctrl+Break
Application.EnableCancelKey = xlDisabled
' Get the number of sheets
SheetCount = ActiveWorkbook.Sheets.Count
' Redimension the arrays
ReDim SheetNames(1 To SheetCount)
ReDim SheetHidden(1 To SheetCount)
' Store a reference to the active sheet
Set OldActive = ActiveSheet
' Fill array with sheet names
For i = 1 To SheetCount
SheetNames(i) = ActiveWorkbook.Sheets(i).Name
Next i
' Fill array with hidden status of sheets
For i = 1 To SheetCount
SheetHidden(i) = Not ActiveWorkbook.Sheets(i).Visible
' Unhide hidden sheets
If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible = True
Next i
' Sort the array in ascending order
Call BubbleSort(SheetNames)
' Turn off screen updating
Application.ScreenUpdating = False
' Move the sheets
For i = 1 To SheetCount
ActiveWorkbook.Sheets(SheetNames(i)).Move Before:=ActiveWorkbook.Sheets(i)
Next i
' Re-hide sheets
For i = 1 To SheetCount
If SheetHidden(i) Then ActiveWorkbook.Sheets(i).Visible = False
Next i
' Reactivate the original active sheet
OldActive.Activate
End Sub
Private Sub BubbleSort(List() As String)
' Sorts the List array in ascending order
Dim First As Integer, Last As Integer
Dim i As Integer, j As Integer
Dim Temp As String
First = LBound(List)
Last = UBound(List)
For i = First To Last - 1
For j = i + 1 To Last
If List(i) > List(j) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i
End Sub