Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
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?


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Sorting worksheets alphabetically (code)

I copied your code to a button and came up with a different error so I'm
assuming the code wasn't posted fully. Anyway to answer your question.
Take out the
Sub SortWorksheets()
and one of the ending End Subs.
HTH

"Ray Ash" wrote in message
...
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?




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Sorting worksheets alphabetically (code)

Private Sub You Rock!()
It worked! You rock! Thanks for the help!
End Sub
-----Original Message-----
I copied your code to a button and came up with a

different error so I'm
assuming the code wasn't posted fully. Anyway to answer

your question.
Take out the
Sub SortWorksheets()
and one of the ending End Subs.
HTH

"Ray Ash" wrote in

message
...
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?




.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting Alphabetically Vicki Excel Worksheet Functions 3 February 6th 10 12:25 AM
sorting using letters , but not alphabetically. Romileyrunner1 Excel Worksheet Functions 5 September 8th 09 08:08 PM
Sorting columns alphabetically SueG Excel Worksheet Functions 3 August 25th 08 07:18 PM
Sorting Alphabetically GOMErvine Excel Discussion (Misc queries) 1 October 23rd 06 08:16 AM
Sorting data in a cell alphabetically Newbie1092 Excel Worksheet Functions 6 December 28th 05 05:50 PM


All times are GMT +1. The time now is 04:01 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"