Mega Code Archive

 
Categories / VisualBasic Script / Excel
 

Sort all worksheets

Sub SortAllSheets()     Dim wb As Workbook     Dim ws As Worksheet     Dim rng As Range     Dim cSheets As Integer     Dim sSheets() As String     Dim i As Integer     Set wb = ActiveWorkbook          cSheets = wb.Sheets.Count     ReDim sSheets(1 To cSheets)          For i = 1 To cSheets       sSheets(i) = wb.Sheets(i).Name     Next          Set ws = wb.Worksheets.Add     For i = 1 To cSheets       ws.Cells(i, 1).Value = sSheets(i)     Next          ws.Columns(1).Sort Key1:=ws.Columns(1), _        Order1:=xlAscending          For i = 1 To cSheets       sSheets(i) = ws.Cells(i, 1).Value     Next          Application.DisplayAlerts = False     ws.Delete     Application.DisplayAlerts = True          For i = 1 To cSheets       wb.Sheets(sSheets(i)).Move After:=wb.Sheets(cSheets)     Next      End Sub