If I have 6 sheets in my excel document named and arranged in this sequence : O1, O2, O3, O1_#2,O2#2,O3#3
Will it be possible to write a macro to change the sequence of these sheets? This is the sequence I am looking for: O1, O1#2, O2, O2#2, O3, O3#2
Thanks
If I understand you correctly, you need to sort sheets in alphabetical order:
Sub SortSheets()
Dim shNames As Collection
Dim i As Long, j As Long
Dim temp As String
Dim sh As Worksheet
Set shNames = New Collection
'add sheet names in collection
For Each sh In ThisWorkbook.Worksheets
shNames.Add sh.Name, sh.Name
Next sh
'bubble sort
For i = 1 To shNames.Count - 1
For j = i + 1 To shNames.Count
If shNames(i) > shNames(j) Then
temp = shNames(j)
shNames.Remove j
shNames.Add temp, temp, i
End If
Next j
Next i
' move sheets
For i = shNames.Count - 1 To 1 Step -1
Worksheets(shNames(i)).Move Before:=Sheets(1)
Next i
End Sub
来源:https://stackoverflow.com/questions/21146857/re-order-excel-sheets-based-on-sheet-names-via-a-macro