|  

» Sort the worksheets in a workbook using VBA in Microsoft Excel

VBA macro tip contributed by Erlandsen Data Consulting offering Microsoft Excel Application development, template customization, support and training solutions
The following macro will sort all the worksheets in a workbook in ascending order :
Sub SortWorksheets()
' sort worksheets in a workbook in ascending order
Dim sCount As Integer, i As Integer, j As Integer
    Application.ScreenUpdating = False
    sCount = Worksheets.Count
    If sCount = 1 Then Exit Sub
    For i = 1 To sCount - 1
        For j = i + 1 To sCount
            If Worksheets(j).Name < Worksheets(i).Name Then
                Worksheets(j).Move Before:=Worksheets(i)
            End If
        Next j
    Next i
End Sub
If you want to sort all the sheets in the workbook, replace worksheets with sheets.


Rate This Tip
12 34 5
Rating: 4.43     Views: 31311
No comments have been submitted.
Click here to post comment
For Registered Users
Name
Comment Title
Comments