Alphabetizing The Worksheets

fdegree

New Member
Joined
Jul 13, 2008
Messages
24
As you all know, when you open a workbook, there are tabs in the lower left corner that show the names of each worksheet within that workbook. My question is…How can I sort these worksheets (tabs) into alphabetical order?


Thanks...
 

Some videos you may like

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Lewiy

Well-known Member
Joined
Jan 5, 2007
Messages
4,284
Try this macro:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Rich (BB code):
<o:p></o:p>
Rich (BB code):
Rich (BB code):
Sub SortTabs()<o:p></o:p>
Dim CheckStr1 As String, CheckStr2 As String<o:p></o:p>
Dim sh As Worksheet<o:p></o:p>
Dim SheetNames() As String<o:p></o:p>
Dim NoOfSheets As Long<o:p></o:p>
Dim c As Long, d As Long<o:p></o:p>
Dim Anyswaps As Boolean<o:p></o:p>
NoOfSheets = ThisWorkbook.Sheets.Count<o:p></o:p>
ReDim SheetNames(NoOfSheets)<o:p></o:p>
If NoOfSheets > 1 Then<o:p></o:p>
    c = 1<o:p></o:p>
    For Each sh In ThisWorkbook.Worksheets<o:p></o:p>
        SheetNames(c) = sh.Name<o:p></o:p>
        c = c + 1<o:p></o:p>
    Next sh<o:p></o:p>
    Do<o:p></o:p>
        Anyswaps = False<o:p></o:p>
        For c = 1 To NoOfSheets - 1<o:p></o:p>
            If SheetNames(c) > SheetNames(c + 1) Then<o:p></o:p>
                SheetNames(0) = SheetNames(c)<o:p></o:p>
                SheetNames(c) = SheetNames(c + 1)<o:p></o:p>
                SheetNames(c + 1) = SheetNames(0)<o:p></o:p>
                Anyswaps = True<o:p></o:p>
            End If<o:p></o:p>
        Next c<o:p></o:p>
    Loop Until Anyswaps = False<o:p></o:p>
    For d = 1 To NoOfSheets<o:p></o:p>
        For c = NoOfSheets To 2 Step -1<o:p></o:p>
            Sheets(SheetNames(c)).Move after:=Sheets(SheetNames(c - 1))<o:p></o:p>
        Next c<o:p></o:p>
    Next d<o:p></o:p>
End If<o:p></o:p>
End Sub<o:p></o:p>
<o:p></o:p>
 

schielrn

Well-known Member
Joined
Apr 4, 2007
Messages
6,939
This should work also:

Code:
Sub sortWS()
Dim ws As Worksheet
Sheets.Add after:=Sheets(Sheets.Count)
Set ws = ActiveSheet
    For i = 1 To Sheets.Count - 1
        ws.Cells(i, 1).Value = Sheets(i).Name
    Next i
    ws.Range("A1:A" & Sheets.Count - 1).Sort key1:=Range("A1")
    For i = 1 To Sheets.Count - 1
        Sheets(ws.Cells(i, 1).Value).Move Before:=Sheets(i)
    Next i
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
End Sub
 

fdegree

New Member
Joined
Jul 13, 2008
Messages
24
Thank you everyone...it's working great.

Kind of interesting how there is more than 1 approach to solving these issues. At least it is interesting to me...I don't know anything about it.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,569
Messages
5,596,918
Members
414,110
Latest member
docops

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top