|
-
May 24th, 2005, 12:52 PM
#1
Thread Starter
Junior Member
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
-
May 24th, 2005, 12:56 PM
#2
Re: VBA code for Excel: Sorting by multiple columns at once
Welcome to the Forums.
Here is about all that you need.
VB Code:
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 01:14 PM
#3
Thread Starter
Junior Member
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"
-
May 24th, 2005, 01:17 PM
#4
Thread Starter
Junior Member
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?
-
May 24th, 2005, 01:18 PM
#5
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 01:20 PM
#6
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 01:25 PM
#7
Thread Starter
Junior Member
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.
-
May 24th, 2005, 01:50 PM
#8
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 02:09 PM
#9
Thread Starter
Junior Member
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?
-
May 24th, 2005, 02:19 PM
#10
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 02:40 PM
#11
Addicted Member
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
-
May 24th, 2005, 03:01 PM
#12
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 24th, 2005, 05:56 PM
#13
Thread Starter
Junior Member
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
-
May 24th, 2005, 07:21 PM
#14
Thread Starter
Junior Member
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...
-
May 24th, 2005, 09:00 PM
#15
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2005, 12:18 PM
#16
Thread Starter
Junior Member
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.
-
May 25th, 2005, 02:13 PM
#17
Thread Starter
Junior Member
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?
-
May 25th, 2005, 02:27 PM
#18
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:
.Sort(Key1, Order1, Key2, Type, Order2, Key3, Order3, Header, OrderCustom, MatchCase, Orientation, SortMethod
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
May 25th, 2005, 02:58 PM
#19
Thread Starter
Junior Member
Re: VBA code for Excel: Sorting by multiple columns at once
Thanks Rob, that did the trick! I really appreciate it.
-
May 25th, 2005, 03:13 PM
#20
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|