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
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
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]
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,725
Members
448,987
Latest member
marion_davis

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