Same code - different results

S

scott_hanebutt

I have found a macro that I am using in Microsoft Project that uses excel.
It alphabatizes the worksheets. It runs fine the first time. If I run it
again without exiting excel it does not sort the worksheets but does not
produce an error. If I exit excel and run it again I get an error that says
run-time error '1004': Method 'Worksheets of object'_Global' failed. If I
then run it again without exiting Excel it does not work but does not produce
an error. If I exit Excel and run it agian it works fine.

I have pasted the sub below.

Private Sub SortWorksheets()

Dim N As Integer
Dim M As Integer
Dim FirstWSToSort As Integer
Dim LastWSToSort As Integer

FirstWSToSort = 3
LastWSToSort = xlApp.Worksheets.Count

For M = FirstWSToSort To LastWSToSort
For N = M To LastWSToSort
If UCase(Worksheets(N).Name) < UCase(Worksheets(M).Name) Then
Worksheets(N).Move before:=Worksheets(M)
End If
Next N
Next M

End Sub

Thanks,
Scott Hanebutt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top