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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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