Sort Worksheets Alphanumerically by Name Using VBA

Written by kazamraza no comments
Classified in : Visual Basic for Application (VBA), Macros Tags : none

I have an Excel workbook with more than 30 sheets. I want these sheets to be sorted in alphanumerically order. Following is the macro that has resolved the issue.

VBA Macro

Sub SortSheetsByTabName()
  'disable screen updating
  Application.ScreenUpdating = False
    'define variables
    Dim CountSheets As Integer, i As Integer, j As Integer
      'count total number of sheets
      CountSheets = Sheets.Count
        'start the outer loop
        For i = 1 To CountSheets - 1
           'start the inner loop and check sheet name
          For j = i + 1 To CountSheets
			'move the sheets in acending order, if you want decending order just change the < (less than) sign to > (greater than) sign
            If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then
              Sheets(j).Move before:=Sheets(i)
            End If
          Next j
        Next i
    'enable screen updating
  Application.ScreenUpdating = True
End Sub