How to sort worksheets tabs automatically with VBA. In situations where you have to manage hundreds of employees or items, for example, it’s a good idea to sort the worksheets tabs. Sorting the worksheets tabs manually will be difficult but with some simple VBA code we can automate the process of sorting the worksheets. Using VBA we can sort the worksheets tabs in ascending or descending order quickly and easily. Watch the video below:
Here’s the complete VBA code to sort worksheet tabs quickly and automatically:
Sub sortWorksheets()
Application.ScreenUpdating = False
Dim shtCount As Integer, i As Integer, j As Integer
Dim Response As VbMsgBoxResult
Response = MsgBox(“Select Yes for Ascending and No for Descending sort order”, vbYesNoCancel)
shtCount = Sheets.Count
For i = 1 To shtCount – 1
For j = i + 1 To shtCount
If Response = vbYes Then
If UCase(Sheets(j).Name) < UCase(Sheets(i).Name) Then
Sheets(j).Move before:=Sheets(i)
End If
ElseIf Response = vbNo Then
If UCase(Sheets(j).Name) > UCase(Sheets(i).Name) Then
Sheets(j).Move before:=Sheets(i)
End If
End If
Next j
Next i
Application.ScreenUpdating = True
End Sub
Download a sample file for practice: