I have a workbook that I a having difficulty in sorting the worksheets. I want to sort the sheets starting after sheet 3 which is called "Master". I had it working where it sorted it first time that I ran the macro but the when a new sheet was inserted after the Master sheet then the sort all went wrong.
Here's the current code and I have attached an example workbook. Would appreciate some help
Sub Sort_Worksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
SortDescending = True 'Lower Nearest Left
'SortDescending = False 'Higest Nearest Left
If ActiveWindow.SelectedSheets.Count = 1 Then
FirstWSToSort = 4 'Start Sort at this sheet
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index <> .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If
For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If SortDescending = True Then
If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M
'
Sheets("Register").Select
End Sub
Bookmarks