Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Excel Sheets and Charts

Status
Not open for further replies.

tbg130

Programmer
Aug 11, 2004
46
CA
Hi All,

I have a project that exports data from Access to Excel then creates charts as 'Worksheets' or Full Page Charts and my sort worksheet routine no longer works... It worked fine with just worksheets, but when added the 'charts' it started failing...

The error is 'subscript out of range'. It errors on 'w.Move Before:=objbook.Worksheets(w.Index)' line...

Any ideas on what could be wrong would be much appreciated.

Code:

Public Sub WorkSheetSort()

On Error GoTo SkipSort

For Each wnull In objbook.Worksheets
prevname = ""
For Each w In objbook.Worksheets
If prevname <> "" Then
If w.Name < prevname Then
w.Move Before:=objbook.Worksheets(w.Index)
Else
prevname = w.Name
End If
Else
prevname = w.Name
End If
Next
Next
SkipSort:

End Sub

Thanks everyone!

Tyler
 
Tyler,

Use Sheets instead of Worksheets
Code:
Public Sub WorkSheetSort()

    On Error GoTo SkipSort
    
    For Each wnull In objbook.Sheets
    prevname = ""
    For Each w In objbook.Sheets
        If prevname <> "" Then
            If w.Name < prevname Then
                w.Move Before:=objbook.Sheets(w.Index)
            Else
                prevname = w.Name
            End If
        Else
            prevname = w.Name
        End If
    Next
    Next
SkipSort:

End Sub


Skip,

[glasses] [red]Be advised:[/red] Researchers have found another Descartes trueism, "Cogito ergo spud."
"I think; therefore, I YAM!
[tongue]
 
Tyler
Obviously Skip has given the solution you were looking for but, just for the hell of it, I had a look at the code you started with.

It didn't work for me, didn't sort the sheets at all!
Shouldn't the line
w.Move Before:=objbook.Sheets(w.Index)
be
w.Move Before:=objbook.Sheets(wnull.Index)
??????

Or is it just me (again!)?

;-)
If a man says something and there are no women there to hear him, is he still wrong? [ponder]
How do I get the best answers?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top