Results 1 to 6 of 6

Thread: How to arrange Worksheets using Macros?

  1. #1
    Join Date
    Feb 2009
    Posts
    40

    How to arrange Worksheets using Macros?

    hi,

    I have created and saved number of worksheets in Excel and would like them to be arranged in a properly specifed manner - ascending or descending order. I want to ask how can I arrange these Excel worksheets ? Is it possible to do this using macros....kindly suggest

  2. #2
    Join Date
    Apr 2008
    Posts
    4,642

    Re: How to arrange Worksheets using Macros?

    Here's the macro code for you -

    Code:
    Sub Sort_Worksheets() 
    
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    
       iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
         & "Clicking No will sort in Descending Order", _
         vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
       For i = 1 To Sheets.Count
          For j = 1 To Sheets.Count - 1
    
             If iAnswer = vbYes Then
                If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
                End If
    
             ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
    
                End If
             End If
    
          Next j
       Next i
    
    End Sub

  3. #3
    Join Date
    Apr 2008
    Posts
    4,642

    Re: How to arrange Worksheets using Macros?

    The above macro will sort all of the worksheets in the current workbook. It can sort in ascending or descending order. This type of sort is conducted through a bubble sort type.

    This is ideal when you are working with a large number of tabs or worksheets in Excel documents and you need to put them in order.

  4. #4
    Join Date
    May 2008
    Posts
    4,570

    Re: How to arrange Worksheets using Macros?

    Excel does not provide a mechanism or command to sort or otherwise order worksheets within a workbook. There are number of VBA functions that you can use to sort or reorder worksheets. With these functions, you can -

    • Sort some or all worksheet by name, in either ascending or descending order.
    • Order the worksheets by the names provided in an array.
    • Order the worksheets in either ascending or descending order based on a cell reference in each worksheet.
    • Group worksheet by tab color (Excel 2002 and later only).
    • Order worksheets based on sheet names in a range of cells.


    You can download a module file containing all the required code and functions for sorting all the Worksheet tabs in a Excel workbook.

  5. #5
    Join Date
    Feb 2010
    Posts
    1

    Re: How to arrange Worksheets using Macros?

    Quote Originally Posted by Eric B View Post
    Here's the macro code for you -

    Code:
    Sub Sort_Worksheets() 
    
    Dim i As Integer
    Dim j As Integer
    Dim iAnswer As VbMsgBoxResult
    
       iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
         & "Clicking No will sort in Descending Order", _
         vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
       For i = 1 To Sheets.Count
          For j = 1 To Sheets.Count - 1
    
             If iAnswer = vbYes Then
                If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
                End If
    
             ElseIf iAnswer = vbNo Then
                If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
                   Sheets(j).Move After:=Sheets(j + 1)
    
                End If
             End If
    
          Next j
       Next i
    
    End Sub
    The code above works great, but what change do I need to make to the code if I want the sheets to sort in the following order 1, 2, 3, ... 10, 11, 12,... 100, 101, 102 followed by any sheets with letters such as W51, W52, W53

    Any help would be greatly appreciated.

    Thanks in advance!

    Robert

  6. #6
    Join Date
    May 2008
    Posts
    4,831

    Re: How to arrange Worksheets using Macros?

    If you want better information on how you can sort your sheet in nubers order is by going on the net ther you will find more information if you do not understand the above coding. You can also visit the microsoft official site for your quiry for better result. Re-arranging worksheet with macros is not a big deal.

Similar Threads

  1. cannot tab between worksheets in Excel 2007
    By MartinaMiranda in forum Windows Software
    Replies: 5
    Last Post: 06-04-2011, 08:43 PM
  2. Linking cells in different worksheets
    By Zool in forum Windows Software
    Replies: 3
    Last Post: 01-08-2009, 01:40 PM
  3. Compare two Excel Worksheets on Mac
    By Claudius in forum Windows Software
    Replies: 2
    Last Post: 10-06-2009, 10:46 PM
  4. Merging Excel worksheets
    By Alexxx in forum Tips & Tweaks
    Replies: 3
    Last Post: 03-03-2009, 08:06 PM

Tags for this Thread

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
  •  
Page generated in 1,714,330,061.66992 seconds with 17 queries