How to Sort Excel Worksheets Alphabetically
Hi friends,
I want to sort my employees name in worksheets should be displayed in alphabetically order. I try to check order and sort in a workbook_sheetselectionchange with macro, but I'm not sure where to put it, or how to write the code. Can anyone help me out to write code for it.
Re: How to Sort Excel Worksheets Alphabetically
Try to use the following code i hope it will work for you.
Code:
Sub SortWorksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
Dim AscDesc
AscDesc = MsgBox(prompt:="Sort worksheets in ascending order?", _
Title:="Sort Order", _
Buttons:=vbYesNoCancel)
If AscDesc = vbYes Then
SortDescending = False
ElseIf AscDesc = vbNo Then
SortDescending = True
Else
Exit Sub
End If
If ActiveWindow.SelectedSheets.Count = 1 Then
FirstWSToSort = 1
LastWSToSort = Worksheets.Count
Else
With ActiveWindow.SelectedSheets
For N = 2 To .Count
If .Item(N - 1).Index <> .Item(N).Index - 1 Then
MsgBox "You cannot sort non-adjacent sheets"
Exit Sub
End If
Next N
FirstWSToSort = .Item(1).Index
LastWSToSort = .Item(.Count).Index
End With
End If
For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If SortDescending = True Then
If UCase(Worksheets(N).Name) > UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M
End Sub
Re: How to Sort Excel Worksheets Alphabetically
:thumbup1:
Well, what the wonderful scripts are!
But I am not a programmer and will be confused by the long and complicated scripts. Now, about the sort worksheets issue, I would like to provide a most somple way to do it. The application I found is Kutools for Excel , which is an excel add-ins collection to free from time consuming and repetitive excel work.Very small size. After installing the app, it adds a "Kutools" tab in the ribbon, and then just clcik "worksheet manager" and choose "sort worksheets", then your sheets are sorted in alphabetical or numeric order. Is that so easy? I still keep using the app and find more useful functions in it.