Sorting Tabs

Nadia1182

New Member
Joined
May 23, 2011
Messages
5
Hello,

I was wondering if there is a macro I can use to sort tabs in numberical order.

Thank you :)
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Found on another website...

Code:
[COLOR=#0000ff]Option Explicit[/COLOR] 
 
[COLOR=blue]Sub[/COLOR] SortWorksheets() 
 
    [COLOR=blue]Dim[/COLOR] N [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] 
    [COLOR=blue]Dim[/COLOR] M [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] 
    [COLOR=blue]Dim[/COLOR] FirstWSToSort [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] 
    [COLOR=blue]Dim[/COLOR] LastWSToSort [COLOR=blue]As[/COLOR] [COLOR=blue]Integer[/COLOR] 
    [COLOR=blue]Dim[/COLOR] SortDescending [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR] 
 
    SortDescending = [COLOR=blue]False[/COLOR] 
 
    [COLOR=blue]If[/COLOR] ActiveWindow.SelectedSheets.Count = 1 [COLOR=blue]Then[/COLOR] 
 
         [COLOR=darkgreen]'Change the 1 to the worksheet you want sorted first[/COLOR]
        FirstWSToSort = 1 
        LastWSToSort = Worksheets.Count 
    [COLOR=blue]Else[/COLOR] 
        [COLOR=blue]With[/COLOR] ActiveWindow.SelectedSheets 
            [COLOR=blue]For[/COLOR] N = 2 [COLOR=blue]To[/COLOR] .Count 
                [COLOR=blue]If[/COLOR] .Item(N - 1).Index <> .Item(N).Index - 1 [COLOR=blue]Then[/COLOR] 
                    MsgBox "You cannot sort non-adjacent sheets" 
                    Exit [COLOR=blue]Sub[/COLOR] 
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
            [COLOR=blue]Next[/COLOR] N 
            FirstWSToSort = .Item(1).Index 
            LastWSToSort = .Item(.Count).Index 
        [COLOR=blue]End With[/COLOR] 
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
 
    [COLOR=blue]For[/COLOR] M = FirstWSToSort [COLOR=blue]To[/COLOR] LastWSToSort 
        [COLOR=blue]For[/COLOR] N = M [COLOR=blue]To[/COLOR] LastWSToSort 
            [COLOR=blue]If[/COLOR] SortDescending = [COLOR=blue]True[/COLOR] [COLOR=blue]Then[/COLOR] 
                [COLOR=blue]If[/COLOR] UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) [COLOR=blue]Then[/COLOR] 
                    Worksheets(N).Move Before:=Worksheets(M) 
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
            [COLOR=blue]Else[/COLOR] 
                [COLOR=blue]If[/COLOR] UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) [COLOR=blue]Then[/COLOR] 
                    Worksheets(N).Move Before:=Worksheets(M) 
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
        [COLOR=blue]Next[/COLOR] N 
    [COLOR=blue]Next[/COLOR] M 
 
[COLOR=blue]End Sub[/COLOR]
 
Upvote 0
Thank you kidwispa for your help. I am trying your macro and am getting the following error message.

"You can not sort non-adjacent sheets."

What could I be doing wrong?
 
Upvote 0
The tabs I have are numbered from 10000 – 10123 yet they are all mixed up. I want to put them in order numerically. Perhaps since this macro alphabetizes this is why it isn’t working?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
 
Upvote 0
Thanks Peter, this would be perfect however I don't have admin rights on my computer so I can't download the software.

A macro is my only work around.
 
Upvote 0

Forum statistics

Threads
1,224,588
Messages
6,179,743
Members
452,940
Latest member
rootytrip

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