order worksheets alphabetically???

  • Thread starter Thread starter ali
  • Start date Start date
A

ali

Hi,

Is it possible to use a macro to order sheets alphabetically? Sounds
simple (much like myself!!)

Thanks guys and gals :-)
 
The following code will sort the sheets in the workbook, in alphabetica
order.
*************************************************
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 = 1
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 ascendin
order. To sort in descending order, you can change SortDescending to
True. You may not want to sort all of the sheets. For example, i
you have a summary sheet at either the beginning or end of th
workbook, you may not want to include this in the sort. To start th
sort after the one or more sheets, change the value of FirstWSToSort t
the index number of the first worksheet to sort. For example, to leav
the first two worksheets in place, change the value to 3. Similarly
to leave the last two sheets in place, change the value of LastWSToSor
to Worksheets.Count.
If you don't know what the worksheet index number is, or you want t
use the worksheet name instead of the index number, you can retriev
the sheet's index number with the Index property. For example,
FirstWSToSort = Worksheets("SomeSheet").Inde
 
There is an example how to do this in John Walkenbach's book "Excel
2002 with VBA". See his site.
 
Back
Top