Sort workseets by color and then alphabetically

jiddings

Board Regular
Joined
Nov 22, 2008
Messages
135
I've located the following color sort VBA that sorts worksheets by color and it is working well.
I'm trying to modify the code to sort each of the "color grouped" sheets alphabetically. That is, sort by color and then each color group sorted alphabetically.

I think I'm in over my head on this one and would greatly appreciate any help.


Code:
Sub SortWorksheetsByColor(Optional ByVal SortByAsc As Boolean = True)
    
    Dim i           As Long
    Dim j           As Long
    Dim ShtC()      As Long
    Dim ShtN()      As String
    Dim t, n        As Long
    Dim lngSU       As Long
    

    With Application
        lngSU = .ScreenUpdating
        .ScreenUpdating = False
    End With
    
    If Val(Application.Version) >= 10 Then
        With ThisWorkbook
            For i = 1 To .Worksheets.Count
                If .Worksheets(i).Visible = -1 Then
                    n = n + 1
                    ReDim Preserve ShtC(1 To n)
                    ReDim Preserve ShtN(1 To n)
                    ShtC(n) = .Worksheets(i).Tab.Color
                    ShtN(n) = .Worksheets(i).Name
                End If
            Next
            For i = 1 To n
                For j = i To n
                    If ShtC(j) < ShtC(i) Then
                        t = ShtN(i)
                        ShtN(i) = ShtN(j)
                        ShtN(j) = t
                        t = ShtC(i)
                        ShtC(i) = ShtC(j)
                        ShtC(j) = t
                    End If
                Next
            Next
            If SortByAsc Then
                For i = n To 1 Step -1
                    .Worksheets(CStr(ShtN(i))).Move before:=.Worksheets(1)
                Next
            Else
                For i = n To 1 Step -1
                    .Worksheets(CStr(ShtN(i))).Move after:=.Worksheets(.Worksheets.Count)
                Next
            End If
        End With
    End If
    
    Application.ScreenUpdating = lngSU
    
End Sub
'Call the routine like
'
'For ascending
'Code:
'SortWorksheetsByColor True
'
'for descending
'Code:
'SortWorksheetsByColor False
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Well, I don't have time right now to see the code,
but sorting by alphabets THEN the colors would solve the problem.
Provided that the worksheets that are being moved are being inserted instead of swapping.
 
Upvote 0
Well, I don't have time right now to see the code,
but sorting by alphabets THEN the colors would solve the problem.
Provided that the worksheets that are being moved are being inserted instead of swapping.

I have tab name alphabetical sorting (employee name - approx. 60 employees) set initially. Then each employee is assigned to one of three managers (different tab color). In each case, the tabs (worksheets) are swapped or moved not inserted for the color sorting. As a result of color sorting (using the mentioned VBA), the resultant color groups are not sorted alphabetically. This is my task / goal to obtain alphabetically sorted color groups for each manager.
Is this possible or am I incorrectly aproaching the problem??
 
Upvote 0
Does anyone have any thoughts on how I can accomplish the alphabetical sort of color grouped tabs??
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

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