Sort worksheet in a numerical order

lhf081

New Member
Joined
Feb 13, 2005
Messages
9
Hi,

I have read several posts where it is possible to sort worksheets in an alphabetical order.

I would like to sort worksheets where there are 15 sheets named let's say 'Sheet1', 'Sheet2',..., 'Sheet15'.

Using the following code that I seen in another thread:


Sub Test1()
Dim i As Integer, j , As Integer

For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
If UCase(Sheets(j).Name) > UCase(Sheets(j + 1).Name) Then Sheets(j).Move After:=Sheets(j + 1)
Next j
Next i

The results was the following sorting:
Sheet1, Sheet10, Sheet11, Sheet12, Sheet13, Sheet14, Sheet15, Sheet2, Sheet3,..., Sheet9.

My question is would it be possible to sort worksheet in a numerical order
i.e.

1,2,3,4,5,....15.

Thanks in advance for your help.
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi, welcome to the board!

Give this a try:

Code:
Sub Test1()
Dim i As Integer, j As Integer
For i = 1 To Sheets.Count
For j = 1 To Sheets.Count - 1
If Val(Replace(UCase(Sheets(j).Name), "SHEET", "")) > Val(Replace(UCase(Sheets(j + 1).Name), "SHEET", "")) Then Sheets(j).Move After:=Sheets(j + 1)
Next j
Next i
End Sub
 
Upvote 0
Hi
try:
Code:
Sub SortSheet()
Dim i As Integer
    For i = 1 To Sheets.Count
        Sheets("sheet" & i).Move after:=Sheets(Sheets.Count)
    Next
End Sub
rgds,
jindon
 
Upvote 0
That will only work if no sheets are ever deleted, are always sheet1, sheet2, sheet3, etc. what if there was a Sheet0?
 
Upvote 0
HOTPEPPER said:
That will only work if no sheets are ever deleted, are always sheet1, sheet2, sheet3, etc. what if there was a Sheet0?
Thanks a lot Hotpepper.

It does the job perfectly.

Regards

lhf081
 
Upvote 0
Hi

that's right!

different aproach
Code:
Sub SortSheet()
Dim i As Integer, ii As Long, a() As String, ws As Worksheet, x, y, b As Variant
    ReDim a(1 To Sheets.Count)
    For Each ws In Sheets
        x = Replace(ws.Name, "Sheet", "")
        i = i + 1
        a(i) = x
    Next
    y = Application.Min(a)
    ReDim b(1 To Sheets.Count)
    ii = 0
    Do
        For i = LBound(a) To UBound(a)
            If a(i) = y Then: ii = ii + 1: b(ii) = a(i): Exit For
        Next
        y = y + 1
    Loop While ii <= Sheets.Count - 1
    For i = 1 To Sheets.Count
        Sheets("Sheet" & b(i)).Move after:=Sheets(Sheets.Count)
    Next
    Erase a, b
End Sub
rgds,
jindon
 
Upvote 0
If those sheets are "linked" by means of interdependent formulas and you have lots of them, the worksheet calculation sequence could be critical and sorting it the way you want won't improve the performance. If the performance suffers, try to run FastExcel (from DecisionModels.com) on your workbook in order to obtain an optimized ordering of your sheets.
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,215
Members
448,554
Latest member
Gleisner2

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