+ Reply to Thread
Results 1 to 3 of 3

Worksheet Sort - but all sheets in array do not exist

  1. #1

    Worksheet Sort - but all sheets in array do not exist

    2003

    Thanks to Chuck Pearson I have the following VBA code:

    Sub SortWS3()

    Dim SortOrder As Variant
    Dim Ndx As Long
    SortOrder = Array("CSheet", "ASheet", "BSheet")
    For Ndx = UBound(SortOrder) To LBound(SortOrder) Step -1
    Worksheets(SortOrder(Ndx)).Move before:=Worksheets(1)
    Next Ndx

    End Sub

    The challenge is that each workbook may not have all of the sheets.

    What I need is a way for the array to SKIP the non-existant sheets OR
    an alternative approach.

    Any thoughts are appreciated

    Thanks EagleOne

  2. #2
    JLatham
    Guest

    RE: Worksheet Sort - but all sheets in array do not exist

    I think maybe the easiest way would be to simply ignore the error created if
    you try to move a sheet that doesn't exist, and then do some error cleanup if
    it does happen.

    On Error Resume Next ' for 'in line' error testing
    For Ndx = Ubound(SortOrder) To LBound(SortOrder) Step -1
    Worksheets(SortOrder(Ndx).Move before:=Worksheets(1)
    If Err <> 0 then
    Err.clear ' assume sheet didn't exist
    End If
    Next
    On Error GoTo 0 ' no more error trapping.

    "[email protected]" wrote:

    > 2003
    >
    > Thanks to Chuck Pearson I have the following VBA code:
    >
    > Sub SortWS3()
    >
    > Dim SortOrder As Variant
    > Dim Ndx As Long
    > SortOrder = Array("CSheet", "ASheet", "BSheet")
    > For Ndx = UBound(SortOrder) To LBound(SortOrder) Step -1
    > Worksheets(SortOrder(Ndx)).Move before:=Worksheets(1)
    > Next Ndx
    >
    > End Sub
    >
    > The challenge is that each workbook may not have all of the sheets.
    >
    > What I need is a way for the array to SKIP the non-existant sheets OR
    > an alternative approach.
    >
    > Any thoughts are appreciated
    >
    > Thanks EagleOne
    >


  3. #3

    Re: Worksheet Sort - but all sheets in array do not exist

    Works great!! Thanks

    JLatham <[email protected]> wrote:

    >On Error Resume Next ' for 'in line' error testing
    >For Ndx = Ubound(SortOrder) To LBound(SortOrder) Step -1
    > Worksheets(SortOrder(Ndx).Move before:=Worksheets(1)
    > If Err <> 0 then
    > Err.clear ' assume sheet didn't exist
    > End If
    >Next


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1