View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Ray Ash Ray Ash is offline
external usenet poster
 
Posts: 2
Default Sorting worksheets alphabetically (code)

Hi

I found some code on the Internet that is *supposed* to
sort my worksheets alphabetically. I created a command
button and entered the following code into it:

Private Sub CommandButton1_Click()
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 UCase(Worksheets(N).Name) UCase(Worksheets
(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets
(M).Name) Then
Worksheets(N).Move Befo=Worksheets(M)
End If
End If
Next N
Next M

End Sub

End Sub

Anyway, whenever I click the command button to do the
sorting, I get the following error: "Compile error:
Expected end sub" The following is highlighted in the
code window: Private Sub CommandButton1_Click(). Any
idea what I'm doing wrong here?