How is this VBA code breaking my worksheet?

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:

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:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Forum statistics

Threads
1,203,460
Messages
6,055,556
Members
444,797
Latest member
18ecooley

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top