VBA Code to Alphabetize Between Two Tabs

OilEconomist

Board Regular
Joined
Dec 26, 2016
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
Thanks in advance as I'll try to post feedback.

How do I alphabetize the sheets that are between the "Start" and "End" sheets. I have sheets before Start and End, but I want them to stay where they are. Now what's happening is all the sheets are being alphabetized.

Code:
Sub Alp()

 '_______________________________________________________________________________________________
        'Turn on alerts, screen updates, and calculate
            'Turn On Display Alerts
                Application.DisplayAlerts = True

            'Turn on Screen Update
                Application.ScreenUpdating = True

            'Turn off Automatic Calculations
                Calculate


 Dim Sht_Start As Long
            Dim Sht_End As Long
            Dim i As Long
            Dim j As Long
            

'Alphabetize
            Sht_Start = Worksheets("Start").Index - 1
            Sht_End = Worksheets("End").Index - 1
            
            
            For i = Sht_Start To Sht_End
            
                    For j = Sht_Start To Sht_End - 1
                    
                        If UCase$(Application.Sheets(j).Name) > _
                            UCase$(Application.Sheets(j + 1).Name) Then
                            
                                Sheets(j).Move after:=Sheets(j + 1)
                                
                        End If
                        
                    Next
                    
                Next

 '_______________________________________________________________________________________________
        'Turn on alerts, screen updates, and calculate
            'Turn On Display Alerts
                Application.DisplayAlerts = True

            'Turn on Screen Update
                Application.ScreenUpdating = True

            'Turn off Automatic Calculations
                Calculate
                
                

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,427
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
VBA Code:
Sub AlphabetizeSheets()
Dim Sht_Start As Long, Sht_End As Long, i As Long, j As Long
Sht_Start = Worksheets("Start").Index
Sht_End = Worksheets("End").Index
Application.ScreenUpdating = False
For i = Sht_Start + 1 To Sht_End - 1
    For j = Sht_Start + 1 To Sht_End - 2
        If UCase$(Application.Sheets(j).Name) > _
            UCase$(Application.Sheets(j + 1).Name) Then
                Sheets(j).Move after:=Sheets(j + 1)
        End If
    Next j
Next i
Application.ScreenUpdating = True
End Sub
 

OilEconomist

Board Regular
Joined
Dec 26, 2016
Messages
226
Office Version
  1. 2019
Platform
  1. Windows
@JoeMo that worked like a charm. Thanks so much and thanks for using as much of my original code as possible.
 

Watch MrExcel Video

Forum statistics

Threads
1,129,754
Messages
5,638,170
Members
417,011
Latest member
Amaden95

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
Top