简体   繁体   中英

Sort excel column in DESCENDING order

Here is my code that correctly sorts the entire sheet in ascending order based on column 2.

$table = $worksheet.ListObjects | where DisplayName -EQ "User_Table"
$table.Sort.SortFields.clear()
$table.Sort.SortFields.add($table.Range.Columns.Item(2))
$table.Sort.apply() 

I've tried adding xlDescending in various places but keep getting syntax errors back.

According to this ... https://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.sortfields.add.aspx ...

SortField Add(
Range Key,
Object SortOn,
Object Order,
Object CustomOrder,
Object DataOption
)

Order is the third argument. Combine that with the following answer ... https://stackoverflow.com/a/11770103/5040941 and you should be able to do something along the lines of

$table.Sort.SortFields.add($table.Range.Columns.Item(2),0,2)

where 0 and 2 are the values of XlSortOnValues and XlDescending

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM