Sorting tabs

ElvisSteel

Board Regular
Joined
Mar 23, 2009
Messages
122
Hi,

I have a large workbook with lots of sheets and I need to be able to sort the sheets so they are alphabetical from left to right, i.e. sheet "AA", then "AB", then "BA" etc.
Is there a way to do this?

Thanks

Steve
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,808
Office Version
  1. 365
Platform
  1. Windows
Hi Steve

Try;

Code:
[COLOR="Blue"]Public[/COLOR] [COLOR="Blue"]Sub[/COLOR] SortSheets()
    [COLOR="Blue"]Const[/COLOR] lngMaxChars [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR] = 30
    
    [COLOR="Blue"]Dim[/COLOR] lngChar [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] varNum [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Variant[/COLOR]
    [COLOR="Blue"]Dim[/COLOR] wks1 [COLOR="Blue"]As[/COLOR] Worksheet, wks2 [COLOR="Blue"]As[/COLOR] Worksheet
    [COLOR="Blue"]Dim[/COLOR] strName1 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR], strName2 [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]String[/COLOR]
    
    [COLOR="Blue"]If[/COLOR] ThisWorkbook.Worksheets.Count = 1 [COLOR="Blue"]Then[/COLOR] [COLOR="Blue"]GoTo[/COLOR] Finish
    
    [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] wks1 [COLOR="Blue"]In[/COLOR] ThisWorkbook.Worksheets
        varNum = ""
        [COLOR="Blue"]For[/COLOR] lngChar = [COLOR="Blue"]Len[/COLOR](wks1.Name) [COLOR="Blue"]To[/COLOR] 1 [COLOR="Blue"]Step[/COLOR] -1
            [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] [COLOR="Blue"]IsNumeric[/COLOR]([COLOR="Blue"]Mid[/COLOR](wks1.Name, lngChar, 1)) [COLOR="Blue"]Then[/COLOR]
                [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
            [COLOR="Blue"]Else[/COLOR]
                varNum = [COLOR="Blue"]Mid[/COLOR](wks1.Name, lngChar, 1) & varNum
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]Next[/COLOR] lngChar
        strName1 = [COLOR="Blue"]Left$[/COLOR](wks1.Name, [COLOR="Blue"]Len[/COLOR](wks1.Name) - [COLOR="Blue"]Len[/COLOR](varNum))
        strName1 = strName1 & [COLOR="Blue"]Format$[/COLOR]([COLOR="Blue"]Val[/COLOR](0 & varNum), Application.Rept(0, lngMaxChars - [COLOR="Blue"]Len[/COLOR](strName1)))
        [COLOR="Blue"]For[/COLOR] [COLOR="Blue"]Each[/COLOR] wks2 [COLOR="Blue"]In[/COLOR] ThisWorkbook.Worksheets
            varNum = ""
            [COLOR="Blue"]For[/COLOR] lngChar = [COLOR="Blue"]Len[/COLOR](wks2.Name) [COLOR="Blue"]To[/COLOR] 1 [COLOR="Blue"]Step[/COLOR] -1
                [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]Not[/COLOR] [COLOR="Blue"]IsNumeric[/COLOR]([COLOR="Blue"]Mid[/COLOR](wks2.Name, lngChar, 1)) [COLOR="Blue"]Then[/COLOR]
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]Else[/COLOR]
                    varNum = [COLOR="Blue"]Mid[/COLOR](wks2.Name, lngChar, 1) & varNum
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            [COLOR="Blue"]Next[/COLOR] lngChar
            strName2 = [COLOR="Blue"]Left$[/COLOR](wks2.Name, [COLOR="Blue"]Len[/COLOR](wks2.Name) - [COLOR="Blue"]Len[/COLOR](varNum))
            strName2 = strName2 & [COLOR="Blue"]Format$[/COLOR]([COLOR="Blue"]Val[/COLOR](0 & varNum), Application.Rept(0, lngMaxChars - [COLOR="Blue"]Len[/COLOR](strName2)))
            [COLOR="Blue"]If[/COLOR] [COLOR="Blue"]StrComp[/COLOR](strName1, strName2, 1) = 1 [COLOR="Blue"]Then[/COLOR]
                wks1.Move after:=wks2
            [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
        [COLOR="Blue"]Next[/COLOR] wks2
    [COLOR="Blue"]Next[/COLOR] wks1
Finish:
    [COLOR="Blue"]Set[/COLOR] wks1 = [COLOR="Blue"]Nothing[/COLOR]
    [COLOR="Blue"]Set[/COLOR] wks2 = [COLOR="Blue"]Nothing[/COLOR]
[COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 

Watch MrExcel Video

Forum statistics

Threads
1,123,177
Messages
5,600,160
Members
414,367
Latest member
dw970906

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