Excel Macro to Sort Worksheets in Alphabetical Order

Excel Macro to Sort Worksheets in Alphabetical Order

February 28, 2017

Microsoft was some VBA code posted to its support site here:

https://support.microsoft.com/en-us/help/812386/how-to-sort-worksheets-alphanumerically-in-a-workbook-in-excel

. .. which will allow you to sort the worksheets of an Excel spreadsheet in alphabetical order.    If you have an Excel workbook like this:

 

 

. . . enter Visual Basic by pressing ALT + F11, and then put this code in a new module:

 

Sub Sort_Active_Book()
Dim i As Integer
Dim j As Integer
Dim iAnswer As VbMsgBoxResult
'
' Prompt the user as which direction they wish to
' sort the worksheets.
'
   iAnswer = MsgBox("Sort Sheets in Ascending Order?" & Chr(10) _
     & "Clicking No will sort in Descending Order", _
     vbYesNoCancel + vbQuestion + vbDefaultButton1, "Sort Worksheets")
   For i = 1 To Sheets.Count
      For j = 1 To Sheets.Count - 1
'
' If the answer is Yes, then sort in ascending order.
'
         If iAnswer = vbYes Then
            If UCase$(Sheets(j).Name) > UCase$(Sheets(j + 1).Name) Then
               Sheets(j).Move After:=Sheets(j + 1)
            End If
'
' If the answer is No, then sort in descending order.
'
         ElseIf iAnswer = vbNo Then
            If UCase$(Sheets(j).Name) < UCase$(Sheets(j + 1).Name) Then
               Sheets(j).Move After:=Sheets(j + 1)
            End If
         End If
      Next j
   Next i
End Sub

 

 

After you press play, the macro will prompt you to either sort the worksheets in ascending or descending order.

 

 

Click yes or no, to get the needed results:

 

 

 

 

Please reload

Contact Me With Your Litigation Support Questions:

seankevinoshea@hotmail.com

  • Twitter Long Shadow

© 2015 by Sean O'Shea . Proudly created with Wix.com