Consulting

Results 1 to 7 of 7

Thread: Ordering worksheet tabs in a specific order

  1. #1
    VBAX Regular
    Joined
    May 2020
    Posts
    5
    Location

    Ordering worksheet tabs in a specific order

    Hi - First time poster. Please let me know if I am doing something incorrectly.
    I have a file that will have a varying number of tabs. There will be Summary tabs and Detail tabs. I need to put the summary tab next to the appropriate Detail tab. Summary tabs will always start with Sum_(and then a tabname also will vary). The Detail tabs will start with Detail_(and then the tabname). So, the tabs will come in like this: SUM_1, SUM_X, Sum_Oth, Detail_1, Detail_X, Detail_Oth.
    I need it to look like this: Sum_1, Detail_1, Sum_X, Detail_X, Sum_Oth, Detail_Oth.
    Attached Files Attached Files

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Option Explicit
    
    
    Sub SortTheTabs()
        Dim i As Long, j As Long, o As Long, n As Long
        Dim arySum() As String, sHold As String, sDetail As String
        Dim ws As Worksheet
        
        ReDim arySum(1 To ThisWorkbook.Worksheets.Count / 2)
    
    
        i = 1
        For Each ws In ThisWorkbook.Worksheets
            If Left(ws.Name, 3) = "Sum" Then
                arySum(i) = ws.Name
                i = i + 1
            End If
        Next
        
        For i = 1 To UBound(arySum) - 1
            For j = i + 1 To UBound(arySum)
                If arySum(i) > arySum(j) Then
                    sHold = arySum(i)
                    arySum(i) = arySum(j)
                    arySum(j) = sHold
                End If
            Next j
        Next i
    
    
        o = 1
        For i = 1 To UBound(arySum)
            
            sDetail = "Detail_" & Right(arySum(i), Len(arySum(i)) - 4)
            Worksheets(arySum(i)).Move before:=Worksheets(o)
            Worksheets(sDetail).Move after:=Worksheets(arySum(i))
        
            o = o + 2
        Next i
    
    
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    May 2020
    Posts
    5
    Location
    Thanks Paul -- the more I see the more i need to learn. I did get a subscript out of range error on the following line
    ReDim arySum(1 To ThisWorkbook.Worksheets.Count / 2)

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Based on your example, I assumed that there was an even number of worksheet, each "Sum_" matched with a "Detail_", and there were no other sheets

    If not true, then I can adjust
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Regular
    Joined
    May 2020
    Posts
    5
    Location
    There should be an even number. It may not always be what I provided in File1_Before.xlsx, but for every Sum_ there will be a Detail_. I tried to run it using the FILE1_BEFORE.XLSX file.

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    I used your original Before file in #1 and added my macro
    Attached Images Attached Images
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Regular
    Joined
    May 2020
    Posts
    5
    Location
    Thanks Paul...Not sure what I did but I so appreciate your help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •