How do I reorder sheets in specific order, then alphabetically?

3

I need to reorder my sheets in a specific order, then if there are any left, do it alphabetically. I have the macro below to reorder them alphabetically.

If i have sheets ""METALS", "SVOC", "GENCHEM", etc.-- I want those to always be in alphabetical order. THEN any other sheets "Apple", "zebra", "Lion" should come after in alphabetical order.

I tried this code, but didn't work out

Sheets("GENCHEM").Move Before:=Sheets(1)
Sheets("METALS").Move Before:=Sheets(2)
Sheets("PCBS").Move Before:=Sheets(3)
Sheets("OC_PEST").Move Before:=Sheets(4)
Sheets("SVOC").Move Before:=Sheets(5)
Sheets("VOC").Move Before:=Sheets(6)

'-------My working macro below----

Option Explicit
Sub reordersheets()
'---Reorders the Sheets---
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean

SortDescending = False

If ActiveWindow.SelectedSheets.Count = 1 Then

    FirstWSToSort = 1
    LastWSToSort = Worksheets.Count
Else
    With ActiveWindow.SelectedSheets
        For N = 2 To .Count
            If .Item(N - 1).Index <> .Item(N).Index - 1 Then
                MsgBox "You cannot sort non-adjacent sheets"
                Exit Sub
            End If
        Next N
        FirstWSToSort = .Item(1).Index
        LastWSToSort = .Item(.Count).Index
    End With
End If

For M = FirstWSToSort To LastWSToSort
    For N = M To LastWSToSort
        If SortDescending = True Then
            If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
                Worksheets(N).Move Before:=Worksheets(M)
            End If
        Else
            If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
                Worksheets(N).Move Before:=Worksheets(M)
            End If
        End If
    Next N
Next M

End Sub

Jburkley

Posted 2017-02-15T16:01:36.547

Reputation: 31

Welcome to the site. What version of Excel (assuming that is what you are using)? – CharlieRB – 2017-02-15T16:18:56.643

Your question is ambiguous - your sample orders PCBS in front of OC_PEST - that's not alphabetically. – LotPings – 2017-02-15T17:08:03.207

Answers

0

I sort of re-did the code. This works for me. Note I kind of "Brute Force" the special sheets you need at the start, using an array.

Option Base 1

Sub t()
Dim shtArray() As String
Dim i       As Long, k As Long
Dim ws      As Worksheet
Dim R       As Range
Dim n       As Long

' Let's "brute force" your specific sheets to the front
Dim exceptionSheets() As Variant
exceptionSheets = Array("GENCHEM", "METALS", "OC_PEST", "PCBS", "SVOC", "VOC")

For i = 1 To ActiveWorkbook.Sheets.Count
    If Not UBound(Filter(exceptionSheets, ActiveWorkbook.Sheets(i).Name)) > -1 Then
        k = k + 1
        Debug.Print Sheets(i).Name
        ReDim Preserve shtArray(k)
        shtArray(k) = ActiveWorkbook.Sheets(i).Name
    End If
Next i

Application.ScreenUpdating = False
'  Thanks to http://www.cpearson.com/excel/SortingArrays.aspx
' create a new sheet
Set ws = ThisWorkbook.Worksheets.Add

' put the array values on the worksheet
Set R = ws.Range("A1").Resize(UBound(shtArray) - LBound(shtArray) + 1, 1)
R = Application.Transpose(shtArray)

' sort the range
R.Sort key1:=R, order1:=xlAscending, MatchCase:=False

' load the worksheet values back into the array
For n = 1 To R.Rows.Count
    shtArray(n) = R(n, 1)
Next n

' delete the temporary sheet
Application.DisplayAlerts = False
ws.Delete
Application.DisplayAlerts = True
Application.ScreenUpdating = True

' Now, sort the sheets.
For i = UBound(exceptionSheets) To 1 Step -1
    Sheets(exceptionSheets(i)).Move after:=Sheets(1)
Next i

For i = UBound(shtArray) To LBound(shtArray) Step -1
    Sheets(shtArray(i)).Move after:=Sheets(7 + i - 1)
Next i

End Sub

BruceWayne

Posted 2017-02-15T16:01:36.547

Reputation: 2 508

0

I used the following code. '' Sub SortWorksheetsTabs() Application.ScreenUpdating = False Dim ShCount As Integer, i As Integer, j As Integer ShCount = Sheets.Count For i = 1 To ShCount - 1 For j = i + 1 To ShCount If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then Sheets(j).Move before:=Sheets(i) End If Next j Next i Application.ScreenUpdating = True End Sub ''

Ron

Posted 2017-02-15T16:01:36.547

Reputation: 1