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.
 

Some videos you may like

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,958
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
That will only work if no sheets are ever deleted, are always sheet1, sheet2, sheet3, etc. what if there was a Sheet0?
 

lhf081

New Member
Joined
Feb 13, 2005
Messages
9

ADVERTISEMENT

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
 

jindon

MrExcel MVP
Joined
Aug 21, 2004
Messages
16,995
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
 

Aladin Akyurek

MrExcel MVP
Joined
Feb 14, 2002
Messages
85,201
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,328
Members
414,054
Latest member
Sameer50

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