Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Excel on 2 Columns? 1

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Can anybody clear this up for me. I believe I have the right context for sorting Excel on multiple criteria but it is failing on me. Can anyone assist? Thanks in advance.

Code:
	Set x = CreateObject("Excel.Application")
	x.Visible = true
	Set newBook = x.Workbooks.Add
	newBook.Worksheets(1).Activate
	x.Cells(1,1).Value = "Last Name"
	x.Cells(1,2).Value = "First Name"
	x.Cells(2,1).Value = "Smith"
	x.Cells(2,2).Value = "James"
	x.Cells(3,1).Value = "Smith"
	x.Cells(3,2).Value = "Bob"
	x.Cells(4,1).Value = "Adams"
	x.Cells(4,2).Value = "Sam"
	
	Set objRange = newBook.Worksheets(1).UsedRange
	Set objRange2 = newBook.Worksheets(1).Range("A1")
	Set objRange3 = newBook.Worksheets(1).Range("B1")
	objRange.Sort objRange2,xlAscending,objRange3,,xlAscending,,,xlYes
	        [green]'Sort Key1, Order1, Key2, Type, Order2, Key3, Order3, Header[/green]
 



Extra COMMA...
Code:
    objRange.Sort objRange2,xlAscending,objRange3,xlAscending,,,xlYes

Skip,

[glasses] [red][/red]
[tongue]
 
That fails too. The extra comma is for the "type." See the context comment at the end of my original post.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
This worked for me...

Code:
Const xlAscending = 1
Const xlYes = 1

Set x = CreateObject("Excel.Application")
x.Visible = True
Set newBook = x.Workbooks.Add
newBook.Worksheets(1).Activate
x.Cells(1,1).Value = "Last Name"
x.Cells(1,2).Value = "First Name"
x.Cells(2,1).Value = "Smith"
x.Cells(2,2).Value = "James"
x.Cells(3,1).Value = "Smith"
x.Cells(3,2).Value = "Bob"
x.Cells(4,1).Value = "Adams"
x.Cells(4,2).Value = "Sam"

Set objRange = newBook.Worksheets(1).UsedRange

Set objRange2 = newBook.Worksheets(1).Range("A2")
Set objRange3 = newBook.Worksheets(1).Range("B2")
objRange.Sort  objRange2,xlAscending, objRange3,,xlAscending,,,xlYes

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks DM4Ever. I have to ask, are you a Danger Mouse fan? Your handle intrigues me.
 
:) No problem...I got to learn something new and no...not a Danger Mouse fan.



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top