Option Explicit
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
'exits procedure if no active workbook
If ActiveWorkbook Is Nothing Then Exit Sub
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
'determine 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
'MsgBox SheetNames(i) 'this is to verify work
'Debug.Print SheetNames(i) 'this is to verify work in window
Next i
'fill array with hideen 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)
'For i = 1 To SheetCount
' Debug.Print SheetNames(i)
'Next i
'turn off screen updating
Application.ScreenUpdating = False
'move sheets
For i = 1 To SheetCount
ActiveWorkbook.Sheets(SheetNames(i)).Move _
before:=ActiveWorkbook.Sheets(i)
Next i
'rehide 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
Sub BubbleSort(List() As String)
'Sorts the List array in ascending order
Dim First As Integer
Dim Last As Integer
Dim i As Integer
Dim 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 UCase(List(i)) > UCase(List(j)) Then
Temp = List(j)
List(j) = List(i)
List(i) = Temp
End If
Next j
Next i
End Sub