+ Reply to Thread
Results 1 to 8 of 8

Sort just the sheet tabs with numbers on them.

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Question Sort just the sheet tabs with numbers on them.

    Hello!

    I found shg's code and I am trying to adapt it to my workbook.

    Each workbook has a sheet named "Cover" and another labelled "TOC" and the other sheets have a mix of numbers and letters ("4500181 sn1", "4500181 sn2", "4500183", etc.).

    I want to leave the "Cover" and "TOC" and then sort the other sheets that have number (or number/letter) names.

    Here's the code I am working with:

    Sub SortSheets(Optional wkb As Workbook = Nothing, _
                 Optional ByVal iBeg As Long = 1, _
                 Optional ByVal iEnd As Long = 2147483647)
      ' shg 2009-09
      ' Insertion-sorts sheets from iBeg to iEnd
    
      Dim i           As Long
      Dim j           As Long
    
      If wkb Is Nothing Then Set wkb = ActiveWorkbook
    
      With wkb
          If iBeg < 1 Then iBeg = 1
          If iEnd > .Sheets.Count Then iEnd = .Sheets.Count
    
          For i = iBeg + 1 To iEnd
              For j = iBeg To i - 1
              If Sheets(i).Name = "Cover" Then
              ' do nothing
              Else
                  If StrComp(.Sheets(i).Name, .Sheets(j).Name, vbTextCompare) <> 1 Then
                      .Sheets(i).Move Before:=.Sheets(j)
                      Exit For
                  End If
                  End If
              Next j
          Next i
      End With
    End Sub
    Anyone have any ideas?

    Respectfully,

    Lost

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Sort just the sheet tabs with numbers on them.

    Try changing the line of
     If Sheets(i).Name = "Cover" Then
    to
     If Sheets(i).Name = "Cover" OR Sheets(i).Name = "TOC" Then
    Did that help?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Sort just the sheet tabs with numbers on them.

    MarvinP,

    Workbook attached.

    The Cover and TOC always wind up at the right rather than the left.

    ??

    Lost
    Attached Files Attached Files

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Sort just the sheet tabs with numbers on them.

    Add these two lines at the end of the routine.
        Sheets("TOC").Move Before:=Sheets(1)
        Sheets("Cover").Move Before:=Sheets(1
    )

    To discover the above lines of code:
    Create a blank workbook and add a few worksheets.
    Rename the last two tabs as TOC and Cover
    Turn on Macro Recorder
    Drag TOC tab to left
    Drag Cover tab to left
    Turn off Recorder and look at the code.

    The above two lines seem to do what you wanted. To learn VBA simply turn on the Macro Recorder and do what you think you want. Then read the code that has been recorded.

    Hope this helps you to learn.

  5. #5
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Sort just the sheet tabs with numbers on them.

    MarvinP,

    The code gets caught in a crazy loop and you have to CTRL-ALT-DELETE it to stop it. I think it is continually sorting the sheets.



    Lost

    Sub SortSheets(Optional wkb As Workbook = Nothing, _
                 Optional ByVal iBeg As Long = 1, _
                 Optional ByVal iEnd As Long = 2147483647)
      ' shg 2009-09
      ' Insertion-sorts sheets from iBeg to iEnd
    
      Dim i           As Long
      Dim j           As Long
    
      If wkb Is Nothing Then Set wkb = ActiveWorkbook
    
      With wkb
          If iBeg < 1 Then iBeg = 1
          If iEnd > .Sheets.Count Then iEnd = .Sheets.Count
    
          For i = iBeg + 1 To iEnd
              For j = iBeg To i - 1
              If Sheets(i).Name = "Cover" Or Sheets(i).Name = "TOC" Then
              ' do nothing
              Else
                  If StrComp(.Sheets(i).Name, .Sheets(j).Name, vbTextCompare) <> 1 Then
                      .Sheets(i).Move Before:=.Sheets(j)
                      Exit For
                  End If
                  End If
              Next j
          Next i
      End With
      Sheets("TOC").Move Before:=Sheets(1)
      Sheets("Cover").Move Before:=Sheets(1)
    End Sub

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Sort just the sheet tabs with numbers on them.

    Hey Learning,

    I set a breakpoint in the newest code above. From the immediate window did a Call SortSheets and stepped throught he code. It jumped back to code behind "ThisWorkbook" that was event code.

    It looks like you had event code behind ThisWorkbook that was triggered each time through the code so it never ended. Perhaps you could have turned it off using an Application.EnableEvent line but I simply removed the code and it seems to work fine.

    Find the attached.
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Re: Sort just the sheet tabs with numbers on them.

    MarvinP,

    1. Workbook with 4 tabs in this order:

    1000, 101, Cover, TOC

    2. Run the code.

    3. It sorts them like this:

    Cover, TOC, 1000, 101

    I am Googling to see if I can find better code.

    Lost

  8. #8
    Valued Forum Contributor
    Join Date
    02-26-2010
    Location
    Chattanooga, TN
    MS-Off Ver
    Excel 2003/2007/2010/2016
    Posts
    432

    Talking Re: Sort just the sheet tabs with numbers on them.

    MarvinP,

    I found one that seems to work great!

    Sub WorksheetsSortAscending()
    'http://www.erlandsendata.no/english/vba/wb/sortsheets.htm
    ' sort worksheets in a workbook in ascending order
    Dim sCount As Integer, i As Integer, j As Integer
    Application.ScreenUpdating = False
    
    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
    If Worksheets(j).name = "Cover" Or Worksheets(j).name = "TOC" Then
    'do nothing
    Else
    Worksheets(j).Move Before:=Worksheets(i)
    End If
    End If
    Next j
    Next i
    Application.ScreenUpdating = True
    End Sub
    Thanks for your help with this thread!

    Respectfully,

    Lost

+ 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