Sorting sheets in a workbook

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I am using this macro to sort my sheets into alphabetical order in my workbook
VBA Code:
Sub SortWorksheets()

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 Worksheets(j).Name < Worksheets(i).Name Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub
However I want to still keep my "Master" Sheet as the 1st sheet in the workbook
The "Summary" sheet as the second last sheet and
The "Invoice" sheet as the last sheet.
Is there a way to do this?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
How about
VBA Code:
Sub SortWorksheets()

Dim sCount As Integer, i As Integer, j As Integer
    Application.ScreenUpdating = False
    sCount = Worksheets.Count
    If sCount = 1 Then Exit Sub
    For i = 2 To sCount - 3
        For j = i + 1 To sCount - 2
            If Worksheets(j).Name < Worksheets(i).Name Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
How about
VBA Code:
Sub SortWorksheets()

Dim sCount As Integer, i As Integer, j As Integer
    Application.ScreenUpdating = False
    sCount = Worksheets.Count
    If sCount = 1 Then Exit Sub
    For i = 2 To sCount - 3
        For j = i + 1 To sCount - 2
            If Worksheets(j).Name < Worksheets(i).Name Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
        Next j
    Next i
    Application.ScreenUpdating = True
End Sub
Hi Fluff, I am using this code after adding a worksheet to the workbook. In this Case the worksheet may be added as the last sheet or the first sheet and in that case the code above will not work.
 
Upvote 0
In that case just use your code & add a bit at the end to move the other sheets to where you want them.
 
Upvote 0
How about
VBA Code:
    Sheets("Master").Move Sheets(1)
    Sheets("Summary").Move , Sheets(Sheets.Count)
    Sheets("Invoice").Move , Sheets(Sheets.Count)
 
Upvote 0
Solution
How about
VBA Code:
    Sheets("Master").Move Sheets(1)
    Sheets("Summary").Move , Sheets(Sheets.Count)
    Sheets("Invoice").Move , Sheets(Sheets.Count)
Hi Fluff, this works perfectly except for the Master Sheet. The Master sheet is sheet1 but sometimes when I add a sheet like with the name 1 or using lowercase letters in the name, I am getting those in the workbook being inserted before sheet 1.
 
Upvote 0
You need to put those 3 lines after the loop in your code.
 
Upvote 0
Hi Fluff, thanks again for your help. I was calling the macro from the worksheet_Activate module but the moment I moved it into a separate module it works fine
Thanks again for the patience.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,904
Messages
6,122,169
Members
449,070
Latest member
webster33

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top