krazykevin76
Board Regular
- Joined
- Jan 8, 2006
- Messages
- 70
I found this code on the net that sorts worksheets by name. I've slightly modified it by hiding a few worksheets that I do not want sorted. The code works fine for sorting. But, when the code gets through running, the function of my "master" worksheet does not work. My master sheet has a scroll bar inserted. It takes a hard set date and adds 14 days to that date to create a new date, which is the start of our pay periods "every 14 days/two weeks). These dates are referenced by all the sheets that this code sorts. When the sorting code is complete, you can tell the scroll bar is moving, and if you go to its properties you can see that the current value for it is changing, but it's not writing to the specified cell make my calculations change.
Here is the code:
Any help is greatly appreciated!!!!
Here is the code:
Code:
Sub SortWorksheets()
Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer
Dim SortDescending As Boolean
Sheets("Sheet2").Visible = False
Sheets("Blank").Visible = False
Sheets("Master").Visible = False
Sheets("Shifts").Visible = False
SortDescending = False
If ActiveWindow.SelectedSheets.Count = 1 Then
'Change the 1 to the worksheet you want sorted first
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 Before:=Worksheets(M)
End If
Else
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move Before:=Worksheets(M)
End If
End If
Next N
Next M
Sheets("Shifts").Visible = True
Sheets("Master").Visible = True
End Sub
Any help is greatly appreciated!!!!
Last edited by a moderator: