Results 1 to 8 of 8

Thread: How to use the 'sort' function in a worksheet?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Question How to use the 'sort' function in a worksheet?

    hello,

    I'm using an excel worksheet in my app and I wondered how do I use the
    'sort' function to sort my first column alphabeticlly...

    thanks

    Dekel C.

  2. #2
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to use the 'sort' function in a worksheet?

    Code:
        Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    or if you want descending sort ...
    Code:
        Selection.Sort Key1:=Range("A1"), Order1:=xlDescending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the 'sort' function in a worksheet?

    what does the Key1:=Range("A1") stands for?
    Dekel C.

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

    Re: How to use the 'sort' function in a worksheet?

    Thats the column your sorting.
    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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the 'sort' function in a worksheet?

    what do I need to put in the selection variable?
    what does it need to stand for?
    Dekel C.

  6. #6
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to use the 'sort' function in a worksheet?

    Quote Originally Posted by dekelc
    what do I need to put in the selection variable?
    what does it need to stand for?
    The above can be run from within Excel (using VBA)

    If you are manipulating Excel from VB, then you need to first create an Excel object. Assuming you have the Excel library included in your project then then above code just needs to be prefixed with your Excel object name.

    Assuming your object naem is objExcel, then the code would read as :

    Code:
    objExcel.Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
    I'm presuming thats what you meant !

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2000
    Location
    I'm right here!
    Posts
    849

    Re: How to use the 'sort' function in a worksheet?

    yes, but I need to select something first.
    what do I need to select? the column? the worksheet?
    and how do I select it?
    Dekel C.

  8. #8
    Frenzied Member TheBionicOrange's Avatar
    Join Date
    Apr 2001
    Location
    Cardiff, UK
    Posts
    1,818

    Re: How to use the 'sort' function in a worksheet?

    If its column A you need to sort on, then just use the above code.
    You don't need to select anything first. The Range specified in the "key" part of the statement takes care of that.

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