View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Sorting sheets in workbook

Jeff,

Try

Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean

SortDescending = False

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 CInt(Mid(Worksheets(N).Name, 6))
CInt(Mid(Worksheets(M).Name, 6)) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If CInt(Mid(Worksheets(N).Name, 6)) <
CInt(Mid(Worksheets(M).Name, 6)) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub





--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"Jeff" wrote in message
...
I have a workbook that has sheets in it that are sheet1 -
sheet33. I tried to
use the code from http://cpearson.com/excel/sortws.htm The
problem is
that it does not handle the numbers in the sheet in the right
order. It puts
them Sheet1, Sheet10, Sheet11, Sheet12...before it gets to
Sheet2. Any ideas?
Thanks! Jeff