ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Cannot sort Tables in C# (https://www.excelbanter.com/excel-programming/375953-cannot-sort-tables-c.html)

Andrew Walaszek

Cannot sort Tables in C#
 
I am having problems sorting Tables in Excel 2007 using C#. The below method
changes the cursor to ascending in the Table's header sort fields but fails
to sort the cells. Under debugger the SortField and Table ranges passed to
Excel look OK and Apply() executes without errors. Any help will be
appreciated.

Thanks,
Andrew

/// <summary
/// Sort Table in ascending order by the specified keys
/// </summary
/// <param name="inTable"input Table to sort</param
/// <param name="inColumns"input key column names to use</param
public static void SortTableByKeys(Excel.ListObject inTable, string[]
inColumns)
{
inTable.Sort.SortFields.Clear();
foreach (string column in inColumns)
{
inTable.Sort.SortFields.Add(inTable.ListColumns.ge t_Item(column).Range,
Excel.XlSortType.xlSortValues, Excel.XlSortOrder.xlAscending, Type.Missing,
Excel.XlSortDataOption.xlSortNormal);
}
inTable.Sort.SetRange(inTable.Range);
inTable.Sort.Orientation = Excel.XlSortOrientation.xlSortColumns;
inTable.Sort.MatchCase = false;
inTable.Sort.Header = Excel.XlYesNoGuess.xlYes;
inTable.Sort.SortMethod = Excel.XlSortMethod.xlPinYin;
inTable.Sort.Apply();
}



All times are GMT +1. The time now is 03:02 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com