+ Reply to Thread
Results 1 to 12 of 12

vba to sort multiple sheet tabs in a workbook

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    vba to sort multiple sheet tabs in a workbook

    Hi All

    I have a workbook with multiple sheets .

    Looking for fast vba code to sort sheet names alphabetically.

    Also note some sheets are alpha numeric that is 01_15_ Sodium Peroxide Analysis

    Thanks

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: vba to sort multiple sheet tabs in a workbook

    Try this code
    Sub SortSheets()
        Dim I As Integer, J As Integer
    
        For I = 1 To Sheets.Count - 1
            For J = I + 1 To Sheets.Count
                If Val(Sheets(I).Name) > Val(Sheets(J).Name) Then
                    Sheets(J).Move Before:=Sheets(I)
                End If
            Next J
        Next I
    End Sub
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  3. #3
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: vba to sort multiple sheet tabs in a workbook

    Hi JEAN1972,

    Try this Code I saw on Mr. Excel some time ago.)

    Sub Sort_Active_Book()
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    '
    ' Prompt the user as which direction they wish to
    ' sort the worksheets.
    '
       iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
         & "Clicking No will sort in Descending Order", _
         vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
       For i = 1 To Sheets.Count
          For j = 1 To Sheets.Count - 1
    '
    ' If the answer is Yes, then sort in ascending order.
    '
             If iAnswer = vbYes Then
                If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
                End If
    '
    ' If the answer is No, then sort in descending order.
    '
            ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
                  Sheets(j).Move After:=Sheets(j + 1)
               End If
             
          Next j
       Next i
    End Sub
    Regards.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  4. #4
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: vba to sort multiple sheet tabs in a workbook

    Quote Originally Posted by Winon View Post
    Hi JEAN1972,

    Try this Code I saw on Mr. Excel some time ago.)

    Sub Sort_Active_Book()
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    '
    ' Prompt the user as which direction they wish to
    ' sort the worksheets.
    '
       iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
         & "Clicking No will sort in Descending Order", _
         vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
       For i = 1 To Sheets.Count
          For j = 1 To Sheets.Count - 1
    '
    ' If the answer is Yes, then sort in ascending order.
    '
             If iAnswer = vbYes Then
                If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
                End If
    '
    ' If the answer is No, then sort in descending order.
    '
            ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
                  Sheets(j).Move After:=Sheets(j + 1)
               End If
             
          Next j
       Next i
    End Sub
    Regards.
    Hi Winon

    Getting compile error: next without for

    Could you look in

  5. #5
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: vba to sort multiple sheet tabs in a workbook

    Another code (I think for Mr. Jbeaucaire)
    Sub SortSheets()
        Dim Arr As Variant, vTemp As Variant, WS As Worksheet
        Dim I As Long, J As Long, buf As String
    
        For Each WS In Worksheets
            buf = buf & "," & WS.Name
        Next WS
        Arr = Split(Mid(buf, 2, Len(buf)), ",")
    
        For I = LBound(Arr, 1) To UBound(Arr, 1) - 1
            For J = I + 1 To UBound(Arr, 1)
                If Arr(I) > Arr(J) Then
                    vTemp = Arr(I)
                    Arr(I) = Arr(J)
                    Arr(J) = vTemp
                End If
            Next J
        Next I
    
        For I = LBound(Arr, 1) To UBound(Arr, 1)
            Sheets(Arr(I)).Move After:=Sheets(Sheets.Count)
        Next I
    End Sub

  6. #6
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: vba to sort multiple sheet tabs in a workbook

    or maybe so
    Sub tttt()
    Dim wsh As Worksheet, i As Long
    With CreateObject("System.Collections.ArrayList")
        For Each wsh In ThisWorkbook.Worksheets
            .Add wsh.Name
        Next
        .Sort
        For i = 0 To .Count - 2
            Sheets(.Item(i + 1)).Move , Sheets(.Item(i))
        Next
    End With
    End Sub

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: vba to sort multiple sheet tabs in a workbook

    Thanks Mr. Nilem for the code
    and may be
    Sub Sort_Sheets()
        Dim Sh_Name(99), Nw_Sh(99) As Variant, X As Long, I As Long, J As Long
        Dim exchg
        
        X = Worksheets.Count
        For I = 1 To X
            Sh_Name(I) = Sheets(I).Name
            Nw_Sh(I) = Sh_Name(I)
        Next I
        
        For I = 1 To X
            For J = I + 1 To X
                If Nw_Sh(J) < Nw_Sh(I) Then exchg = Nw_Sh(J): Nw_Sh(J) = Nw_Sh(I): Nw_Sh(I) = exchg
            Next J
        Next I
        
        For I = X To 1 Step -1
            Sheets(Nw_Sh(I)).Move Before:=Sheets(1)
        Next I
    End Sub

  8. #8
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: vba to sort multiple sheet tabs in a workbook

    Hello JEAN1972,

    Just for fun. Here is another macro that will sort the worksheets either in ascending (a to z) or descending (z to a) order. This code is a little longer but uses a modified bubble sort which is twice as fast as a normal bubble sort. Doesn't mean much when sorting worksheets but is important when using it to sort lists.

    Sub SortWorksheets(Optional ByVal ZtoA As Boolean)
    
        Dim LB      As Long
        Dim J       As Long
        Dim vList   As Variant
        Dim Sorted  As Boolean
        Dim Temp    As Variant
        Dim UB      As Long
            
        ' Written:  July 15, 2016
        ' Author:   Leith Ross
        ' Summary:  Sorts worksheets in ascending or descending alpahbetical order.
        '           Default is ascending order (ZtoA is False).
    
            ReDim vList(1 To ThisWorkbook.Worksheets.Count)
            
            For n = 1 To ThisWorkbook.Worksheets.Count
                vList(n) = ThisWorkbook.Worksheets(n).Name
            Next n
            
            LB = LBound(vList)
            UB = UBound(vList)
            
                Do
                    Sorted = True
                    
                    For J = LB To UB - 1
                        If ZtoA Xor StrComp(vList(J), vList(J + 1), vbTextCompare) = 1 Then
                            Temp = vList(J + 1)
                            vList(J + 1) = vList(J)
                            vList(J) = Temp
                            
                            Sorted = False
                        End If
                    Next J
                    
                    UB = UB - 1
                Loop Until Sorted Or UB < 1
            
            For n = 1 To ThisWorkbook.Worksheets.Count - 1
                ThisWorkbook.Worksheets(vList(n)).Move Before:=ThisWorkbook.Worksheets(n)
            Next n
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  9. #9
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: vba to sort multiple sheet tabs in a workbook

    Quote Originally Posted by JEAN1972 View Post
    Also note some sheets are alpha numeric that is 01_15_ Sodium Peroxide Analysis
    What the other sheet names look like and how do you want sort them?

  10. #10
    Forum Contributor
    Join Date
    02-11-2014
    Location
    mauritius
    MS-Off Ver
    MS365
    Posts
    1,086

    Re: vba to sort multiple sheet tabs in a workbook

    Quote Originally Posted by jindon View Post
    What the other sheet names look like and how do you want sort them?
    Hi Jindon

    Some examples numeric ones first AND alphabetical last like 01_15__sodium peroxide ,beoh_10a27,alphabetical,reports 2015_a_01IE.

  11. #11
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,588

    Re: vba to sort multiple sheet tabs in a workbook

    Quote Originally Posted by JEAN1972 View Post
    Hi Jindon

    Some examples numeric ones first AND alphabetical last like 01_15__sodium peroxide ,beoh_10a27,alphabetical,reports 2015_a_01IE.
    See if this is how you wanted.
    Sub test()
        Dim ws As Worksheet, i As Long
        With CreateObject("System.Collections.SortedList")
            For Each ws In Sheets
                .Item(GetSortVal(ws.Name)) = ws.Name
            Next
            For i = .Count - 1 To 0 Step -1
                Sheets(.GetByIndex(i)).Move before:=Sheets(1)
            Next
        End With
    End Sub
    
    Function GetSortVal(ByVal txt As String) As String
        Dim i As Long, m As Object
        With CreateObject("VBScript.RegExp")
            .Global = True
            .Pattern = "\d+(\.\d+)?"
            If .test(txt) Then
                For i = .Execute(txt).Count - 1 To 0 Step -1
                    Set m = .Execute(txt)(i)
                    txt = Application.Replace(txt, m.firstindex + 1, m.Length, _
                    Format$(m.Value, String(12, "0") & "," & String(10, "0")))
                Next
            End If
        End With
        GetSortVal = txt
    End Function

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: vba to sort multiple sheet tabs in a workbook

    Try
    Sub Sort_Sheets_Winon()
        Dim I As Integer
        Dim J As Integer
        Dim iAnswer As VbMsgBoxResult
    
        iAnswer = MsgBox("Sort Sheets In Ascending Order?" & Chr(10) & "Clicking No Will Sort In Descending Order", vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
        For I = 1 To Sheets.Count
            For J = 1 To Sheets.Count - 1
                If iAnswer = vbYes Then
                    If UCase$(Sheets(J).Name) > UCase$(Sheets(J + 1).Name) Then
                        Sheets(J).Move After:=Sheets(J + 1)
                    End If
                ElseIf iAnswer = vbNo Then
                    If UCase$(Sheets(J).Name) < UCase$(Sheets(J + 1).Name) Then
                        Sheets(J).Move After:=Sheets(J + 1)
                    End If
                End If
            Next J
        Next I
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] VBA code to merge multiple sheet with multiple tabs into one workbook in different tabs
    By Jagdev in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-06-2015, 07:42 AM
  2. [SOLVED] Sort workbook Tabs only
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2014, 07:39 AM
  3. Replies: 0
    Last Post: 02-05-2014, 05:18 PM
  4. [SOLVED] Extract data from a workbook with multiple tabs, into a workbook with corresponding tabs
    By krackaberr in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-28-2013, 11:54 AM
  5. Is it possible to sort the tabs in a workbook?
    By Jules in forum Excel General
    Replies: 1
    Last Post: 08-03-2006, 02:55 PM
  6. [SOLVED] Can you sort sheet tabs in a workbook
    By Techtrainer in forum Excel General
    Replies: 5
    Last Post: 05-18-2006, 06:15 AM
  7. [SOLVED] selecting multiple sheet tabs and open another workbook
    By Bannor in forum Excel General
    Replies: 5
    Last Post: 11-24-2005, 10:40 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1