[VBA] Unable to sort tabs of selected sheets

smallxyz

Active Member
Joined
Jul 27, 2015
Messages
393
Office Version
  1. 2021
Platform
  1. Windows
Code:
Sub SortSelectedWorksheets()
    '---------------------
    MTxt = MsgBox("- Sort Selected Worksheet(s) -" & vbNewLine & "---------------------" & vbNewLine & _
        "Yes : Ascendingly" & vbNewLine & _
        "No  : Descendingly", vbYesNoCancel)
    '---------------------
    SheetCount = ActiveWindow.SelectedSheets.Count
    '---------------------
    With ActiveWindow
        Select Case (MTxt)
            ' ________________ [ sort ascendingly ] ________________
            Case (vbYes):
                For i = 1 To SheetCount - 1
                    For j = i + 1 To SheetCount
                        If (.SelectedSheets(j).Name < .SelectedSheets(i).Name) Then
                            .SelectedSheets(j).Move before:=.SelectedSheets(i)
                        End If
                    Next
                Next
                
                ' ________________ [ sort descendingly ] ________________
            Case (vbNo):
                For i = 1 To SheetCount - 1
                    For j = i + 1 To SheetCount
                        If (.SelectedSheets(j).Name > .SelectedSheets(i).Name) Then
                            .SelectedSheets(j).Move before:=.SelectedSheets(i)
                        End If
                    Next
                Next
        End Select
        '---------------------
    End With
End Sub

I got an error for the above in sorting 5 selected worksheets.

But I am just unsure which part goes wrong.

Any hint?

Thanks a lot!
 
Last edited:

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
How about
Code:
    sheetcount = ActiveWindow.SelectedSheets.Count
    ReDim Shts(1 To sheetcount)
    For i = 1 To sheetcount
      Shts(i) = ActiveWindow.SelectedSheets(i).Name
    Next i
   
    '---------------------
        Select Case (MTxt)
            ' ________________ [ sort ascendingly ] ________________
            Case (vbYes):
                For i = 1 To UBound(Shts) - 1
                    For j = i + 1 To UBound(Shts)
                        If Shts(j) < Shts(i) Then
                            Sheets(Shts(j)).Move before:=Sheets(Shts(i))
                        End If
                    Next
                Next
                
                ' ________________ [ sort descendingly ] ________________
            Case (vbNo):
As soon as a sheet is moved it becomes the active sheet, at which only have one active sheet, so your code fails
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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