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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel Automation Sort

COM and Automation

Excel Automation Sort

by  jmcd0719  Posted    (Edited  )
*The documentation on sorting a table in Excel is lacking
*The syntax for using the sort object is as follows

public oleapp
oleapp=Createobject("excel.application")

*There are 12 parameters:
lkey1='oleapp.Range("a1")' && Column you want to sort
lorder1=1 && 1 - Ascending 2 - Decending (default 1)
ltype="" && Leave Blank (default blank)
lkey2='oleapp.Range("b1")' && Second Column (default blank)
lorder2=1 && same as above (default 1)
lkey3='oleapp.Range("c1")' && Third Column (default blank)
lorder3=1 && same as above (default 1)
lheader=1 && Does table have Header 0-Guess,1-Yes,2-No (Default 0)
&&lcustom= skip altogher
lmatch=.f. && match case .t.=Yes .f.=NO (default .f.)
&&lorient= Skip altogether
lsortmeth=1 && not sure if used (default 1)
ldataopt1=0 && Sort normally, by month, day, etc (default 0)
ldataopt2=0 && Same as opt1
ldataopt3=0 && Same as opt2

oleapp.Visible=.T.
l_file="c:\test.csv" && any file will do that opens natively in excel"
oleapp.Workbooks.Open(l_file)
lrange="a2:iv65536"
oleapp.Range(lrange).Sort(&lkey1) && Simplest sort using defaults
&& sorting using variables
oleapp.Range(lrange).Sort(&lkey1,lorder1,ltype,&lkey2,lorder2,&lkey3,lorder3,lheader,,lmatch,,lsortmeth,ldataopt1,ldataopt2,ldataopt3)
&& sorting with values
oleapp.Range("a2:iv65536").Sort(oleapp.Range("a1"),1,,oleapp.Range("b1"),1,oleapp.Range("c1"),1,1,,.f.,,1,0,0,0)


Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top