Sheet tabs in the right order?

The following is a recorded procedure that put my sheet tabs in the right order.  However when I insert sheet tabs it messes things up.  Is there a way to put sheet tabs in a specific order based on a sheet tab name?

Sheets("Past Due").Select
    Sheets("Past Due").Move Before:=Sheets(3)
    Sheets("Past Due Bruce").Select
    Sheets("Past Due Bruce").Move Before:=Sheets(4)
    Sheets("Non Past Due").Select
    Sheets("Non Past Due").Move Before:=Sheets(4)
    Sheets("Non Past Due").Select
    Sheets("Non Past Due").Move Before:=Sheets(6)
    Sheets("Recovery Date").Select
    Sheets("Recovery Date").Move Before:=Sheets(6)
    Sheets("Original").Select
    Sheets("Original").Move Before:=Sheets(7)
    Sheets("Orders").Select
    Sheets("Orders").Move Before:=Sheets(8)
    Sheets("Recovery Date").Select

Open in new window

RWayneHAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnBusiness Consultant (Owner)Commented:
When I insert a sheet, I use the Insert Before, End, etc. and the sheet goes where I want. In Excel itself, it does not sort sheet tabs by name. There may be a macro to do it but Excel will not.
0
FlysterCommented:
What is the right order? If you want ascending order you can use this macro:
Sub SortWS()
Dim ct, i, x As Integer
Application.ScreenUpdating = False
  ct = Worksheets.Count
    If ct = 1 Then Exit Sub
      For i = 1 To ct - 1
        For x = i + 1 To ct
          If Worksheets(x).Name < Worksheets(i).Name Then
            Worksheets(x).Move Before:=Worksheets(i)
          End If
        Next x
    Next i
End Sub

Open in new window

Flyster
0
RWayneHAuthor Commented:
I have a bunch of sheet moving around, added and what not.  I thought the instead of going through all the code and checking the place before...  That I could do a global sub at the end that said, Put DSC sheet tab first, put Original sheet tab second... etc.  I delete and insert so many times that placing them by sheet name many be easier.
0
Glenn RayExcel VBA DeveloperCommented:
You can actually reference the sheet name in the Move method.  But you don't need to do that here.

If you want to order the sheets at the end of your processing, this code will sort them.
Sub Order_Sheets()
    Dim arrSheets() As String
    Dim strSheets As String
    Dim x As Integer
    
    'List all sheets in order separated by commas (no space after comma)
    strSheets = "Past Due,Past Due Bruce,Non Past Due,Recovery Date,Original,Orders,Misc"
    arrSheets = Split(strSheets, ",")
    
    For x = 1 To UBound(arrSheets)
        Sheets(arrSheets(x)).Move after:=Sheets(ActiveWorkbook.Sheets.Count)
    Next x
    
End Sub

Open in new window


You just need to create a list of all the worksheets in the order needed (line 7 of the code snippet). You don't have to list them all, but if you don't, the unreferenced sheets will appear before the ordered ones do.

Example file - with unordered sheets - attached.
EE-OrderSheets.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
RWayneHAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.