+ Reply to Thread
Results 1 to 4 of 4

Rearrange sheets on tab numbers

Hybrid View

Jos vd Heijden Rearrange sheets on tab... 05-08-2015, 02:24 AM
Debraj Roy Re: Rearrange sheets on tab... 05-08-2015, 03:05 AM
Trebor76 Re: Rearrange sheets on tab... 05-08-2015, 03:09 AM
Jos vd Heijden Re: Rearrange sheets on tab... 05-08-2015, 04:02 AM
  1. #1
    Registered User
    Join Date
    09-02-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    16

    Rearrange sheets on tab numbers

    Is it possible to rearrange the sheets on the numbers of the tabs.
    I want to have the sheet with the lowest number of the tab on the left to start with.
    Attached Files Attached Files

  2. #2
    Forum Expert Debraj Roy's Avatar
    Join Date
    09-27-2012
    Location
    New Delhi,India
    MS-Off Ver
    Excel 2013
    Posts
    1,469

    Re: Rearrange sheets on tab numbers

    check here for more details..

    http://www.ozgrid.com/VBA/sort-sheets.htm
    Regards!
    =DEC2HEX(3563)

    If you like someone's answer, click the star to give them a reputation point for that answer...

  3. #3
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Rearrange sheets on tab numbers

    Hi Jos,

    There's plenty of references on how to do this on the internet including this article by Pearson Software Consulting.

    That said, I wrote this to sort numeric sheet tabs as in your example as well:

    Option Explicit
    Sub SortNumericTabs()
        
        Dim varMyArray() As Variant 'Declares a dynamic array variable
        Dim lngArrayCount As Long
        Dim wsMySheet As Worksheet
        
        For Each wsMySheet In ThisWorkbook.Sheets
            lngArrayCount = lngArrayCount + 1
            ReDim Preserve varMyArray(1 To lngArrayCount) 'Append the record to the existing array
            varMyArray(lngArrayCount) = Val(wsMySheet.Name)
        Next wsMySheet
        
        varMyArray = BubbleSrt(varMyArray, True) 'True - sort 'varMyArray' Ascending. False - sort 'varMyArray' Decending.
        
        'Loop through Array elements
        For lngArrayCount = 1 To UBound(varMyArray)
            For Each wsMySheet In ThisWorkbook.Sheets
                If wsMySheet.Name = CStr(varMyArray(lngArrayCount)) Then
                    Exit For
                End If
            Next wsMySheet
            If lngArrayCount < ThisWorkbook.Worksheets.Count Then
                wsMySheet.Move before:=ThisWorkbook.Worksheets(lngArrayCount + 1)
            Else
                wsMySheet.Move after:=ThisWorkbook.Worksheets(lngArrayCount)
            End If
        Next lngArrayCount
        
        Erase varMyArray() 'Deletes the varible contents to free some memory
        
    End Sub
    'This UDF is from here: http://www.mrexcel.com/forum/excel-questions/690718-visual-basic-applications-sort-array-numbers.html
    Public Function BubbleSrt(ArrayIn, Ascending As Boolean)
    
        Dim SrtTemp As Variant
        Dim i As Long
        Dim j As Long
    
    
        If Ascending = True Then
            For i = LBound(ArrayIn) To UBound(ArrayIn)
                 For j = i + 1 To UBound(ArrayIn)
                     If ArrayIn(i) > ArrayIn(j) Then
                         SrtTemp = ArrayIn(j)
                         ArrayIn(j) = ArrayIn(i)
                         ArrayIn(i) = SrtTemp
                     End If
                 Next j
             Next i
        Else
            For i = LBound(ArrayIn) To UBound(ArrayIn)
                 For j = i + 1 To UBound(ArrayIn)
                     If ArrayIn(i) < ArrayIn(j) Then
                         SrtTemp = ArrayIn(j)
                         ArrayIn(j) = ArrayIn(i)
                         ArrayIn(i) = SrtTemp
                     End If
                 Next j
             Next i
        End If
        
        BubbleSrt = ArrayIn
    
    End Function
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  4. #4
    Registered User
    Join Date
    09-02-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: Rearrange sheets on tab numbers

    Thanks, everybody.
    This works for me.

+ 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] Rearrange numbers in a cell
    By SYoung88 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-07-2012, 10:10 AM
  2. Rearrange some data sheets
    By psyked in forum Excel General
    Replies: 1
    Last Post: 03-29-2011, 09:31 AM
  3. Rearrange numbers
    By Jordans121 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 03-02-2010, 01:05 AM
  4. rearrange numbers in a cell
    By gunsmokegirl in forum Excel General
    Replies: 11
    Last Post: 08-08-2009, 05:34 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