+ Reply to Thread
Results 1 to 7 of 7

VBA Help to Sort Worksheets

Hybrid View

  1. #1
    Registered User
    Join Date
    02-14-2013
    Location
    wales
    MS-Off Ver
    Excel 2010
    Posts
    64

    VBA Help to Sort Worksheets

    I have a workbook that I a having difficulty in sorting the worksheets. I want to sort the sheets starting after sheet 3 which is called "Master". I had it working where it sorted it first time that I ran the macro but the when a new sheet was inserted after the Master sheet then the sort all went wrong.
    Here's the current code and I have attached an example workbook. Would appreciate some help
    Sub Sort_Worksheets()
    
    Dim N As Integer
    Dim M As Integer
    Dim FirstWSToSort As Integer
    Dim LastWSToSort As Integer
    Dim SortDescending As Boolean
    
    SortDescending = True   'Lower Nearest Left
    'SortDescending = False 'Higest Nearest Left
    
    
    If ActiveWindow.SelectedSheets.Count = 1 Then
        FirstWSToSort = 4 'Start Sort at this sheet
        LastWSToSort = Worksheets.Count
    Else
        With ActiveWindow.SelectedSheets
            For N = 2 To .Count
                If .Item(N - 1).Index <> .Item(N).Index - 1 Then
                    MsgBox "You cannot sort non-adjacent sheets"
                    Exit Sub
                End If
            Next N
            FirstWSToSort = .Item(1).Index
            LastWSToSort = .Item(.Count).Index
         End With
    End If
    
    For M = FirstWSToSort To LastWSToSort
        For N = M To LastWSToSort
            If SortDescending = True Then
                If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
                    Worksheets(N).Move Before:=Worksheets(M)
                End If
            Else
                If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
                   Worksheets(N).Move Before:=Worksheets(M)
                End If
            End If
         Next N
    Next M
    '
    Sheets("Register").Select
    
    End Sub
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: VBA Help to Sort Worksheets

    Hi,

    Try this code which I had on hand (on my PC)
    Adjust as needed...
    It will sort from sheet 4 to the end...

    Sub SortSheets()
    Dim lCount As Long, lCounted As Long
    Dim lShtLast As Long
    Dim lReply As Long
    
    lReply = MsgBox("To sort Worksheets ascending, select 'Yes'. " & vbNewLine & _
        "To sort Worksheets descending select 'No'", vbYesNoCancel + vbInformation, "Sheet Sort")
    If lReply = vbCancel Then Exit Sub
    lShtLast = Sheets.Count
        If lReply = vbYes Then 'Sort ascending
            For lCount = 4 To lShtLast
                For lCount2 = lCount To lShtLast
                    If UCase(Sheets(lCount2).Name) < UCase(Sheets(lCount).Name) Then
                        Sheets(lCount2).Move Before:=Sheets(lCount)
                    End If
                Next lCount2
            Next lCount
        Else 'Sort descending
         For lCount = 4 To lShtLast
                For lCount2 = lCount To lShtLast
                    If UCase(Sheets(lCount2).Name) > UCase(Sheets(lCount).Name) Then
                        Sheets(lCount2).Move Before:=Sheets(lCount)
                    End If
                Next lCount2
            Next lCount
        End If
    End Sub
    Regards,
    Rudi

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA Help to Sort Worksheets

    You could use this macro.
    The LT function tests if the sheet name is numeric, to avoid a purely alphabetic sort where "12" < "2"
    Sub test()
        Dim Descending As Boolean
        Dim i As Long, j As Long
        Application.ScreenUpdating = False
    
        Descending = (MsgBox("Sort Ascending?", vbYesNo) = vbNo)
        
        With ThisWorkbook
            For i = 1 To .Sheets.Count - 1
                For j = i + 1 To .Sheets.Count
                    If LT(.Sheets(j), .Sheets(i)) Xor Descending Then
                        .Sheets(j).Move after:=.Sheets(i)
                        .Sheets(i).Move after:=.Sheets(j)
                    End If
                Next j
            Next i
            
            Sheet3.Move before:=.Sheets(1)
            Sheet2.Move before:=.Sheets(1)
            Sheet1.Move before:=.Sheets(1)
            Application.ScreenUpdating = True
        End With
    End Sub
    
    Function LT(aSheet As Worksheet, bSheet As Worksheet) As Boolean
        If IsNumeric(aSheet.Name) Or IsNumeric(bSheet.Name) Then
            LT = Val(aSheet.Name) < Val(bSheet.Name)
        Else
            LT = aSheet.Name < bSheet.Name
        End If
    End Function
    Last edited by mikerickson; 07-06-2014 at 11:59 AM.
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    02-14-2013
    Location
    wales
    MS-Off Ver
    Excel 2010
    Posts
    64

    Re: VBA Help to Sort Worksheets

    Great job thanks you. I have got my original code to work by replacing the "Ucase" with "Val" and this worked. How would your code look hard coded to descending rather than having the question box

    many thanks Mark

  5. #5
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA Help to Sort Worksheets

    double post

  6. #6
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: VBA Help to Sort Worksheets

    Just set Descending to True.

    Or omit the Descending variable all together and change this line

    If Not LT(.Sheets(j), .Sheets(i)) Then

  7. #7
    Registered User
    Join Date
    02-14-2013
    Location
    wales
    MS-Off Ver
    Excel 2010
    Posts
    64

    Re: VBA Help to Sort Worksheets

    Thanks All

+ 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. sort worksheets alphabetically and keep the data in the worksheets
    By Cynthia Woods in forum Excel - New Users/Basics
    Replies: 2
    Last Post: 05-15-2013, 12:38 PM
  2. Sort all Worksheets
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-30-2010, 09:41 AM
  3. How do I sort Worksheets by name?
    By M Skabialka in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 04-07-2005, 02:06 PM
  4. Sort Worksheets
    By mate in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-24-2005, 07:06 AM
  5. Replies: 1
    Last Post: 03-10-2005, 05:06 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