+ Reply to Thread
Results 1 to 4 of 4

Sort tabs

  1. #1
    Stan
    Guest

    Sort tabs

    Is there a way to sort tabs in the worksheet? I've been manually dragging the
    tabs to put them in the alphabetical order, but if someone can advise a way,
    that would be great.
    Thanks

  2. #2
    Registered User
    Join Date
    04-26-2006
    Posts
    5
    this VBA code should help you achieve what you want.

    Sub SortWorksheets()

    Dim N As Integer
    Dim M As Integer
    Dim FirstWSToSort As Integer
    Dim LastWSToSort As Integer
    Dim SortDescending As Boolean

    SortDescending = False

    If ActiveWindow.SelectedSheets.Count = 1 Then
    FirstWSToSort = 1
    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

    End Sub
    Courtesy of Chip Pearson (www.cpearson.com)
    Last edited by NickG; 04-26-2006 at 11:32 AM.

  3. #3
    Jim Rech
    Guest

    Re: Sort tabs

    Chip Pearson has a macro for sorting worksheets.

    http://www.cpearson.com/excel/sortws.htm

    --
    Jim
    "Stan" <Stan@discussions.microsoft.com> wrote in message
    news:2554F74B-4151-474C-B246-2B733F2491C3@microsoft.com...
    | Is there a way to sort tabs in the worksheet? I've been manually dragging
    the
    | tabs to put them in the alphabetical order, but if someone can advise a
    way,
    | that would be great.
    | Thanks



  4. #4
    Chip Pearson
    Guest

    Re: Sort tabs

    If you're going to take verbatim code from my site, I would
    appreciate a reference to the page.
    www.cpearson.com/excel/sortws.htm


    --
    Cordially,
    Chip Pearson
    Microsoft MVP - Excel
    Pearson Software Consulting, LLC
    www.cpearson.com


    "NickG" <NickG.26vo56_1146065452.4553@excelforum-nospam.com>
    wrote in message
    news:NickG.26vo56_1146065452.4553@excelforum-nospam.com...
    >
    > this VBA code should help you achieve what you want.
    >
    >> Sub SortWorksheets()
    >>
    >> Dim N As Integer
    >> Dim M As Integer
    >> Dim FirstWSToSort As Integer
    >> Dim LastWSToSort As Integer
    >> Dim SortDescending As Boolean
    >>
    >> SortDescending = False
    >>
    >> If ActiveWindow.SelectedSheets.Count = 1 Then
    >> FirstWSToSort = 1
    >> 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
    >>
    >> End Sub

    >
    >
    > --
    > NickG
    > ------------------------------------------------------------------------
    > NickG's Profile:
    > http://www.excelforum.com/member.php...o&userid=33865
    > View this thread:
    > http://www.excelforum.com/showthread...hreadid=536411
    >




+ 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