Results 1 to 20 of 20

Thread: VBA code for Excel: Sorting by multiple columns at once

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    VBA code for Excel: Sorting by multiple columns at once

    Hi,

    I am having trouble finding examples of code to where I can sort an excel spreadshet by multiple columns at one time.

    The code I'm using for sorting one at a time is as follows:

    excelApp.Worksheets("Temp").UsedRange.Sort excelApp.Range("A1"),xlAscending

    What do I have to add to this to sort by column b as well?

    An example of what I'm trying to do...Say there are 50 people divided in to 5 teams. Column A is their team Number and Column B is their last name. I want to sort them first by their Team Number then Second by their last name. This is really easy to do in excel using their sort tool, but I am generating a ton of reports through this code and it would be a pain to have to do this manually for each report.

    Thanks for any help you can provide in advance.

    DL

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    Welcome to the Forums.

    Here is about all that you need.

    VB Code:
    1. Workbooks(1).Sheets(1).Range("A:A").Sort Key1:=Workbooks(1).Sheets(1).Range("A:A"), Order1:=xlAscending, Key2:=Workbooks(1).Sheets(1).Range("B:B"), Order2:=xlAscending, Orientation:=xlSortRows
    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

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Thanks for the help. I'm having a little trouble getting it to compile. What goes where the (1) is? My excel document doesn't have a name yet and the worksheet I'm using is "temp"

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Guess I should have posted this under the vb script board? Is the code much different for vbscript?

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    It is just the positioning of the collection of workbooks and sheets.
    Even if its not saved you can use the excelApp.Worksheets("Temp"). Where Temp is the name of the workbook. "Book1" is the default until its saved.
    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
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: VBA code for Excel: Sorting by multiple columns at once

    Well this sounds like your using VB6 to automate Excel, so this is an ok spot for your thread.

    VBA is the "script" language that Excel and other Office apps use. So if your coding directly behind Excel then the thread would be
    better in the VBA forum.
    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

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Thanks again, So just to make sure I understood you correctly I can do this:

    excelApp.Worksheets("Temp").Range.Sort Key1:=excelApp.Worksheets("Temp").Range("A:A"), Order1:=xlAscending, key2:=excelApp.Worksheets("Temp").Range("B:B"), Order2:=xlAscending, Orientation:=xlSortRows


    -------------
    If it is the case that I understood you correctly, I am getting and "Expected Statement" error.

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    Yes, as long as "temp" is your workbook name.
    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

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Ok, I got it to compile by taking out the := and just using = . When it runs I get a type mismatch error, any ideas?

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    Are you early or late binding? Can you post your code?
    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

  11. #11
    Addicted Member
    Join Date
    Jan 2003
    Posts
    163

    Re: VBA code for Excel: Sorting by multiple columns at once

    alternative: record a macro doing exactly what you want and then import that code

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    Macros are good to determine what methods and function Excel uses for a task, but they usually contain allot of code padding that is not
    needed and will slow down your app.

    I'd take the one line of code verses the several a macro will generate, but in this situation its the same code.
    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

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Not sure what you mean by early or late binding. Here is the code that I am using to do the sort:

    excelApp.Workbooks(1).Sheets(1).UsedRange.Sort Key1=excelApp.Workbooks(1).Sheets(1).Range("C:C"), Order1=xlAscending, key2=excelApp.Workbooks(1).Sheets(1).Range("I:I"), Order2=xlAscending, Orientation=xlSortRows

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    I took code straight from an excel sheet and I still get the type mismatch error...

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    If you didnt add a reference to MS Excel xx.0 Object Library and you create your excel app object like "CreateObject then thats
    late binding. If you added the ref. and you use Set excelApp = New Excel.Application then thats early binding.

    the reason is that if you late bind then you either need to declare your consts or use the numeric values only.
    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

  16. #16

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    I used the following code to declare my object, which I believe you noted as late binding:

    Dim excelApp 'MS Excel Application Object
    'start the word application object
    Set excelApp = CreateObject("Excel.Application")
    excelApp.Visible = false

    How would I change the following statement to account for the late binding?
    excelApp.Worksheets("Temp").UsedRange.Sort Key1:=excelApp.Worksheets("Temp").Range("A:A"), Order1:=xlAscending, key2:=excelApp.Worksheets("Temp").Range("B:B"), Order2:=xlAscending, Orientation:=xlSortRows


    ----------------------------------
    Side note:

    I was able to get in contact with the person that wrote this code, and he gave me the following code and said it should work:

    excelApp.WorkSheets("Temp").UsedRange.Sort excelApp.Range("A1"),xlAscending, excelApp.Range("B1"),xlAscending

    I get the following error:

    Error Number: 1004
    Error Source: Microsoft Office Excel
    Error Description: Reference is not valid

    Is there an easy fix for this? A solution for either should work.

    Thanks for any help in advance!
    Last edited by DL21; May 25th, 2005 at 02:08 PM.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Does it have to be the 1st 2 columns in the spreadsheet?

    The data that I need to have sorted is in columns C and D. Could that be causing the problem?

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    The code they gave you will not work since the range is only for the first row only - A1.

    You can sort on ANY columns you need.

    To make it for Late binding you need to take out the labels and consts. and keep the positioning of the parameters according to the function.
    For Excel 2003:

    VB Code:
    1. .Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod
    2.  
    3. excelApp.Worksheets("Temp").UsedRange.Sort excelApp.Worksheets("Temp").Range("C:C"), 1, excelApp.Worksheets("Temp").Range("D:D"), , 1, , , , , , 2
    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

  19. #19

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    24

    Re: VBA code for Excel: Sorting by multiple columns at once

    Thanks Rob, that did the trick! I really appreciate it.

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

    Re: VBA code for Excel: Sorting by multiple columns at once

    Glad to help. I should have had this solved earlier but I think I am having an off day today. Plus, I'm bored.
    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

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