Cannot sort Tables in C#

A

Andrew Walaszek

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.get_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();
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top