Sort Sheets Alphabetically

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello, so I have the following code which successfully sorts the sheets in a workbook alphabetically when run.

I would like to make a couple of alterations to it. I would like it to:

1. Run automatically upon change to the number of tabs, e.g. when a sheet is added, and
2. Only to apply to sheets after a sheet called "Lookup"

Any thoughts? Many thanks.

Code:
Sub Sort_Tabs_Alphabetically()
 
For i = 1 To Application.Sheets.Count
    For j = 1 To Application.Sheets.Count - 1
        If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
            Sheets(j).Move after:=Sheets(j + 1)
        End If
    Next
Next
 
End Sub
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Hello, this sorts only those after a sheet called "Lookup".

As for running when you add a sheet, I'm sure you mean at the point you rename the sheet?

If so it isn't straight forward. There's no built in event for sheet name change. However I saw this thread that has a work around:

https://stackoverflow.com/questions/1939919/rename-worksheet-event-in-excel

Code:
Sub Sort_Tabs_Alphabetically()


For i = Sheets("Lookup").Index + 1 To Application.Sheets.Count
    For j = Sheets("Lookup").Index + 1 To Application.Sheets.Count - 1
        If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
            Sheets(j).Move after:=Sheets(j + 1)
        End If
    Next
Next
 
End Sub
 
Upvote 0
Hello, this sorts only those after a sheet called "Lookup".

As for running when you add a sheet, I'm sure you mean at the point you rename the sheet?

If so it isn't straight forward. There's no built in event for sheet name change. However I saw this thread that has a work around:

https://stackoverflow.com/questions/1939919/rename-worksheet-event-in-excel

Code:
Sub Sort_Tabs_Alphabetically()


For i = Sheets("Lookup").Index + 1 To Application.Sheets.Count
    For j = Sheets("Lookup").Index + 1 To Application.Sheets.Count - 1
        If UCase$(Application.Sheets(j).Name) > UCase$(Application.Sheets(j + 1).Name) Then
            Sheets(j).Move after:=Sheets(j + 1)
        End If
    Next
Next
 
End Sub

Thanks Gallen, it is OK I can set it to run after a new sheet is created in my preceding macro.
 
Upvote 0

Forum statistics

Threads
1,223,897
Messages
6,175,269
Members
452,628
Latest member
dd2

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
Back
Top