+ Reply to Thread
Results 1 to 4 of 4

[SOLVED] Need Help with a sort

Hybrid View

  1. #1
    Greg Glynn
    Guest

    [SOLVED] Need Help with a sort

    Can anyone see why this code produces an error?

    Windows(ImportFileName).Activate
    ActiveWorkbook.ActiveSheet.Cells.Select
    Selection.Sort Key1:=Range("B1"), Order1:=xlAscending,
    Key2:=Range("C1") _
    , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending,
    Header:=xlNo, OrderCustom:=1, MatchCase:=False,
    Orientation:=xlTopToBottom


    The error is:
    Run-time Error 1004:
    The Sort reference is not valid.

    ... it's driving me batty.

    Thanks in advance.


  2. #2
    NickHK
    Guest

    Re: Need Help with a sort

    It is seldom necessary or desirable to select object in Excel. This works,
    as long as the WB and WS names are valid:

    Application.Workbooks(ImportFileName).Worksheets("Sort").Cells.Sort _
    Key1:=Range("B1"), Order1:=xlAscending, _
    Key2:=Range("C1"), Order2:=xlAscending, _
    Key3:=Range("D1"), Order3:=xlAscending, _
    Header:=xlNo, _
    OrderCustom:=1, _
    MatchCase:=False, _
    Orientation:=xlTopToBottom

    It may be better use .UsedRange or .CurrentRegion for the range to sort,
    depending on layout of you sheet.

    NickHK

    "Greg Glynn" <GregoryGlynn@gmail.com> wrote in message
    news:1153282561.650961.319510@b28g2000cwb.googlegroups.com...
    > Can anyone see why this code produces an error?
    >
    > Windows(ImportFileName).Activate
    > ActiveWorkbook.ActiveSheet.Cells.Select
    > Selection.Sort Key1:=Range("B1"), Order1:=xlAscending,
    > Key2:=Range("C1") _
    > , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending,
    > Header:=xlNo, OrderCustom:=1, MatchCase:=False,
    > Orientation:=xlTopToBottom
    >
    >
    > The error is:
    > Run-time Error 1004:
    > The Sort reference is not valid.
    >
    > .. it's driving me batty.
    >
    > Thanks in advance.
    >




  3. #3
    Dave Peterson
    Guest

    Re: Need Help with a sort

    Just a warning that that worksheet Sort will have to be active (since the keys
    are unqualified).

    Maybe...

    with Workbooks(ImportFileName).Worksheets("Sort")
    .Cells.Sort Key1:=.Range("B1"), Order1:=xlAscending, _
    Key2:=.Range("C1"), Order2:=xlAscending, _
    Key3:=.Range("D1"), Order3:=xlAscending, _
    Header:=xlNo, _
    OrderCustom:=1, _
    MatchCase:=False, _
    Orientation:=xlTopToBottom
    end with

    To the OP, those dots (.range("b1")) mean that the ranges belong to the object
    in the previous With statement.


    NickHK wrote:
    >
    > It is seldom necessary or desirable to select object in Excel. This works,
    > as long as the WB and WS names are valid:
    >
    > Application.Workbooks(ImportFileName).Worksheets("Sort").Cells.Sort _
    > Key1:=Range("B1"), Order1:=xlAscending, _
    > Key2:=Range("C1"), Order2:=xlAscending, _
    > Key3:=Range("D1"), Order3:=xlAscending, _
    > Header:=xlNo, _
    > OrderCustom:=1, _
    > MatchCase:=False, _
    > Orientation:=xlTopToBottom
    >
    > It may be better use .UsedRange or .CurrentRegion for the range to sort,
    > depending on layout of you sheet.
    >
    > NickHK
    >
    > "Greg Glynn" <GregoryGlynn@gmail.com> wrote in message
    > news:1153282561.650961.319510@b28g2000cwb.googlegroups.com...
    > > Can anyone see why this code produces an error?
    > >
    > > Windows(ImportFileName).Activate
    > > ActiveWorkbook.ActiveSheet.Cells.Select
    > > Selection.Sort Key1:=Range("B1"), Order1:=xlAscending,
    > > Key2:=Range("C1") _
    > > , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending,
    > > Header:=xlNo, OrderCustom:=1, MatchCase:=False,
    > > Orientation:=xlTopToBottom
    > >
    > >
    > > The error is:
    > > Run-time Error 1004:
    > > The Sort reference is not valid.
    > >
    > > .. it's driving me batty.
    > >
    > > Thanks in advance.
    > >


    --

    Dave Peterson

  4. #4
    NickHK
    Guest

    Re: Need Help with a sort

    Dave,
    Good catch, I missing that.

    NickHK

    "Dave Peterson" <petersod@verizonXSPAM.net> wrote in message
    news:44BE1EF5.73EC020C@verizonXSPAM.net...
    > Just a warning that that worksheet Sort will have to be active (since the

    keys
    > are unqualified).
    >
    > Maybe...
    >
    > with Workbooks(ImportFileName).Worksheets("Sort")
    > .Cells.Sort Key1:=.Range("B1"), Order1:=xlAscending, _
    > Key2:=.Range("C1"), Order2:=xlAscending, _
    > Key3:=.Range("D1"), Order3:=xlAscending, _
    > Header:=xlNo, _
    > OrderCustom:=1, _
    > MatchCase:=False, _
    > Orientation:=xlTopToBottom
    > end with
    >
    > To the OP, those dots (.range("b1")) mean that the ranges belong to the

    object
    > in the previous With statement.
    >
    >
    > NickHK wrote:
    > >
    > > It is seldom necessary or desirable to select object in Excel. This

    works,
    > > as long as the WB and WS names are valid:
    > >
    > > Application.Workbooks(ImportFileName).Worksheets("Sort").Cells.Sort _
    > > Key1:=Range("B1"), Order1:=xlAscending, _
    > > Key2:=Range("C1"), Order2:=xlAscending, _
    > > Key3:=Range("D1"), Order3:=xlAscending, _
    > > Header:=xlNo, _
    > > OrderCustom:=1, _
    > > MatchCase:=False, _
    > > Orientation:=xlTopToBottom
    > >
    > > It may be better use .UsedRange or .CurrentRegion for the range to sort,
    > > depending on layout of you sheet.
    > >
    > > NickHK
    > >
    > > "Greg Glynn" <GregoryGlynn@gmail.com> wrote in message
    > > news:1153282561.650961.319510@b28g2000cwb.googlegroups.com...
    > > > Can anyone see why this code produces an error?
    > > >
    > > > Windows(ImportFileName).Activate
    > > > ActiveWorkbook.ActiveSheet.Cells.Select
    > > > Selection.Sort Key1:=Range("B1"), Order1:=xlAscending,
    > > > Key2:=Range("C1") _
    > > > , Order2:=xlAscending, Key3:=Range("D1"), Order3:=xlAscending,
    > > > Header:=xlNo, OrderCustom:=1, MatchCase:=False,
    > > > Orientation:=xlTopToBottom
    > > >
    > > >
    > > > The error is:
    > > > Run-time Error 1004:
    > > > The Sort reference is not valid.
    > > >
    > > > .. it's driving me batty.
    > > >
    > > > Thanks in advance.
    > > >

    >
    > --
    >
    > Dave Peterson




+ 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