Archive of Mr Excel Message Board

Back to Excel VBA archive index
Back to archive home

Sorting Spreadsheets

Posted by jennifer on December 13, 2001 8:36 PM
I just got asked this question, and have no clue...

Is there anyway to alphabetically sort worksheets within a workbook?

Check out our Excel VBA Resources

Re: Sorting Spreadsheets

Posted by DaK on December 13, 2001 8:40 PM

Use this code and attach it to a button. The only thing is that sheets with just a number will come first alphabetically.

Public Sub sortsheets()
'sort the workbook sheets

Dim iCount As Integer
Dim i As Integer
Dim j As Integer

On Error Resume Next
iCount = Sheets.Count
For i = 1 To iCount - 1
For j = i + 1 To iCount
If Sheets(j).Name < Sheets(i).Name Then
Sheets(j).Move before:=Sheets(i)
End If
Next j
Next i

End Sub

This archive is from the original message board at
All contents © 1998-2004
Visit our online store to buy searchable CD's with thousands of VBA and Excel answers.
Microsoft Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.