P
Porky79
Hi Guys - I am looking to use the below code listed by Padgett back
in 2004 to order my worksheets alphetically. However I am wondering if
it is possible to modify so that it groups worksheets by colour and
then sorts them alphetibically - to explain I am colour coding pink
for girls and blue for boys and I want the code to result in having
all boys worksheets grouped and sorted alphabetically followed by the
girls grouped and sorted.
Any assistance would be great
Thanks
Paul
Sub SortWorksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
SortDescending = False
FirstWSToSort = 2
LastWSToSort = Worksheets.Count
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
End Sub
*******************************************
As written above, the code will sort all of the worksheets in
ascending
order. To sort in descending order, you can change SortDescending
to
True. You may not want to sort all of the sheets. For example, if
you have a summary sheet at either the beginning or end of the
workbook, you may not want to include this in the sort. To start the
sort after the one or more sheets, change the value of FirstWSToSort
to
the index number of the first worksheet to sort. For example, to
leave
the first two worksheets in place, change the value to 3. Similarly,
to leave the last two sheets in place, change the value of
LastWSToSort
to Worksheets.Count.
If you don't know what the worksheet index number is, or you want to
use the worksheet name instead of the index number, you can retrieve
the sheet's index number with the Index property. For example,
FirstWSToSort = Worksheets("SomeSheet").Index
in 2004 to order my worksheets alphetically. However I am wondering if
it is possible to modify so that it groups worksheets by colour and
then sorts them alphetibically - to explain I am colour coding pink
for girls and blue for boys and I want the code to result in having
all boys worksheets grouped and sorted alphabetically followed by the
girls grouped and sorted.
Any assistance would be great
Thanks
Paul
Sub SortWorksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
SortDescending = False
FirstWSToSort = 2
LastWSToSort = Worksheets.Count
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
End Sub
*******************************************
As written above, the code will sort all of the worksheets in
ascending
order. To sort in descending order, you can change SortDescending
to
True. You may not want to sort all of the sheets. For example, if
you have a summary sheet at either the beginning or end of the
workbook, you may not want to include this in the sort. To start the
sort after the one or more sheets, change the value of FirstWSToSort
to
the index number of the first worksheet to sort. For example, to
leave
the first two worksheets in place, change the value to 3. Similarly,
to leave the last two sheets in place, change the value of
LastWSToSort
to Worksheets.Count.
If you don't know what the worksheet index number is, or you want to
use the worksheet name instead of the index number, you can retrieve
the sheet's index number with the Index property. For example,
FirstWSToSort = Worksheets("SomeSheet").Index