Results 1 to 8 of 8

Thread: *Resolved* Excel VBA Sort Functions

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Resolved *Resolved* Excel VBA Sort Functions

    I am testing an Excel app. that was originally written in Excel 2002 and is also supposed to compatible with Excel 2000. However there is a VBA macro that doesn't seem to work in Excel 2000 (though it works fine in 2002).

    The code sorts a table of data in descending order, but when I try to run the macro I get the error message:

    Run-time error '1004':

    Application-defined or object defined error


    This is the offending peice of VBA code:

    Code:
    Selection.Sort Key1:=Range("K21"), Order1:=xlDescending, Header:=xlGuess _
            , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
    The code is supposed to sort the data table but it just produces an error. The code was recorded in Excel 2002 using the Record Macro function and it would appear there is something in there that Excel 2002 can understand but 2000 can't. Excel 2000 has a Sort function though, so I can't see why this doesn't work.

    Can anyone see anything in that code which is only available to Excel 2002 or above?

    Cheers
    -Rob
    Last edited by TheRobster; Apr 27th, 2005 at 08:21 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    Right....I've tried recording a similar Macro is Excel 2000. This is the code:

    Code:
    Selection.Sort Key1:=Range("I21"), Order1:=xlDescending, Header:=xlGuess _
            , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

    There are some differences in the last part of the code.

    Code:
    DataOption1:=xlSortNormal
    Is this the problem? Should I edit the code so that it complies with the Excel 2000 syntax? I.e. should I delete this extra bit of code? I don't know if this will bugger up the code though.....
    Last edited by TheRobster; Apr 27th, 2005 at 01:37 PM.

  3. #3
    Addicted Member
    Join Date
    Jan 2002
    Location
    Glasgow, Scotland
    Posts
    202

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    take the section of code out and try.

    not sure it would make a difference.

    I had a similar issue with a coded search and it turned out to be a corrupt install.

    re-installed office and it worked ok.
    if you fail to plan, you plan to fail

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    Well I took the final piece of code out and it seems to have solved the problem. Hope it wasn't anything important......the Sort macro works anyway so it obviously didn't need that end bit (finger's crossed).

    If someone could confirm what it actually did I would be grateful.

    I seem to remember having a couple of problems installing Office 2000.......unreadable files or something like that. Could be something to do with that I suppose.

    -Rob

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    The DataOptions are optional. It specifies how to sort text in key 1, 2, 3.

    DataOption1 started with 2002 and in 2003 you get DataOption1, DataOption2, DataOption3.

    You can sort as xlSortTextAsNumbers or xlSortNormal.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    RobDogg,

    I removed the last line of code so now it just looks like this:

    Code:
    Selection.Sort Key1:=Range("K21"), Order1:=xlDescending, Header:=xlGuess _
            , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    It seems to work fine now. Do you think it was okay to delete the DataOption bit? E.g. it seems to work fine now but will this cause problems in the future?

    I haven't changed the code in any other way, apart from deleting the DataOption line......

    Cheers
    -Rob

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    No it should be ok. The only thing that DataOption1 is for specifying the sort order of additional column.
    The key column that the sorting is performed on is the first parameter so your ok.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    May 2004
    Location
    Right here
    Posts
    275

    Re: *Urgent Help Needed* Excel VBA Sort Functions

    Cheers for the help guys.

    -Rob

Posting Permissions

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



Click Here to Expand Forum to Full Width