[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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").Move Before:=Sheets(7)
    Sheets("Orders").Move Before:=Sheets(8)
    Sheets("Recovery Date").Select

Open in new window

1 Solution
John HurstBusiness 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.
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

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.
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.
RWayneHAuthor Commented:

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now