+ Reply to Thread
Results 1 to 4 of 4

Sort Worksheets with numbered sheets first and text sheets last

Hybrid View

  1. #1
    Registered User
    Join Date
    10-30-2007
    Posts
    13

    Sort Worksheets with numbered sheets first and text sheets last

    I need a Macro to sort all sheets in a workbook by name that puts numbered (i.e. 6,7,8,9,10) first and then text names last. (i.e. "Samples", "Content") So it would look like 6,7,8,9,10,20,115,Content, Samples.

    Here is what I have so far that does the numbered sheets correctly but puts the text named sheets first.

    Sub WorksheetsSortAscending()
    ' sort worksheets in a workbook in ascending order
    Dim sCount As Integer, i As Integer, j As Integer
    sCount = Worksheets.Count
    If sCount = 1 Then Exit Sub
    For i = 1 To sCount - 1
    For j = i + 1 To sCount
    If Val(Worksheets(j).Name) < Val(Worksheets(i).Name) Then
    Worksheets(j).Move before:=Worksheets(i)
    End If
    Next j
    Next i
    End Sub

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Sort Worksheets with numbered sheets first and text sheets last

    Try:

    Sub SortSheets()
    Dim I As Integer, ii As Integer
        Dim ShtName As String
        If ActiveWorkbook.Sheets.count > 1 Then
            On Error Resume Next
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            Sheets("SheetNames").Visible = True
            Sheets("SheetNames").Delete
            On Error GoTo 0
            Sheets.Add().Name = "SheetNames"
            Application.DisplayAlerts = True
            With ThisWorkbook.Sheets("SheetNames")
                .Visible = xlSheetVeryHidden
                .Columns(20).Clear
                 
                For I = 1 To ActiveWorkbook.Sheets.count
                    If Sheets(I).Name <> "SheetNames" Then
                        .Cells(I, 20) = Sheets(I).Name
                    End If
                Next
                 
                .Columns(20).Sort Key1:=.Cells(1, 20), Order1:=xlAscending
                 
                For ii = I - 2 To 1 Step -1
                    ShtName = .Cells(ii, 20)
                    ActiveWorkbook.Sheets(ShtName).Move Before:=ActiveWorkbook.Sheets(1)
                Next
                 
            End With
        End If
    Application.ScreenUpdating = True
    End Sub
    Last edited by Mumps1; 01-03-2013 at 03:28 PM.

  3. #3
    Registered User
    Join Date
    10-30-2007
    Posts
    13

    Re: Sort Worksheets with numbered sheets first and text sheets last

    Quote Originally Posted by Mumps1 View Post
    Try:

    Sub SortSheets()
    Dim I As Integer, ii As Integer
        Dim ShtName As String
        If ActiveWorkbook.Sheets.count > 1 Then
            On Error Resume Next
            Application.ScreenUpdating = False
            Application.DisplayAlerts = False
            Sheets("SheetNames").Visible = True
            Sheets("SheetNames").Delete
            On Error GoTo 0
            Sheets.Add().Name = "SheetNames"
            Application.DisplayAlerts = True
            With ThisWorkbook.Sheets("SheetNames")
                .Visible = xlSheetVeryHidden
                .Columns(20).Clear
                 
                For I = 1 To ActiveWorkbook.Sheets.count
                    If Sheets(I).Name <> "SheetNames" Then
                        .Cells(I, 20) = Sheets(I).Name
                    End If
                Next
                 
                .Columns(20).Sort Key1:=.Cells(1, 20), Order1:=xlAscending
                 
                For ii = I - 2 To 1 Step -1
                    ShtName = .Cells(ii, 20)
                    ActiveWorkbook.Sheets(ShtName).Move Before:=ActiveWorkbook.Sheets(1)
                Next
                 
            End With
        End If
    Application.ScreenUpdating = True
    End Sub
    That errors (out of range) when it gets to "ActiveWorkbook.Sheets(ShtName).Move Before:=ActiveWorkbook.Sheets(1)"

    It does begin to resort them though I cant really tell how it was going to do it.

  4. #4
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 365
    Posts
    8,026

    Re: Sort Worksheets with numbered sheets first and text sheets last

    Could you post your file?
    Last edited by Mumps1; 01-03-2013 at 04:42 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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