Sorting Specified workbooks

wmtsub

Active Member
Joined
Jun 20, 2018
Messages
322
How can I sort all worksheets in a work book alphabetically but specify one particular worksheet to be 1st and another last ?
 
Last edited:

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
Sub wmtsub()
  Dim wkb           As Workbook

  Set wkb = ActiveWorkbook
  
  With wkb
    .Worksheets("Alan").Move Before:=.Sheets(1)
    .Worksheets("Zoey").Move After:=.Sheets(.Sheets.Count)
    SortSheets wkb, 2, .Sheets.Count - 1
  End With
End Sub

Sub SortSheets(Optional wkb As Workbook = Nothing, _
               Optional ByVal iBeg As Long = 1, _
               Optional ByVal iEnd As Long = 2147483647)
  ' shg 2009-09
  ' Insertion-sorts sheets from iBeg to iEnd

  Dim i             As Long
  Dim j             As Long

  If wkb Is Nothing Then Set wkb = ActiveWorkbook

  With wkb
    If iBeg < 1 Then iBeg = 1
    If iEnd > .Sheets.Count Then iEnd = .Sheets.Count

    For i = iBeg + 1 To iEnd
      For j = iBeg To i - 1
        If StrComp(.Sheets(i).Name, .Sheets(j).Name, vbTextCompare) <> 1 Then
          .Sheets(i).Move Before:=.Sheets(j)
          Exit For
        End If
      Next j
    Next i
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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