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.
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