RedRocket
RedRocket

Reputation: 1733

How to sort the Excel column numerically

I want to sort a column which contains numeric numbers. In my column E, I have a column of numbers by they aren't sorted properly.

For example: 8, 2, 3, 1, 10

I want 1, 2, 3, 8, 10

Here is my code

    Excel.Range bRange = xlWorkSheet.get_Range("A1", "P1");
    bRange.EntireColumn.AutoFit();
    bRange.Sort(bRange.Columns[5], Excel.XlSortOrder.xlAscending);

Upvotes: 2

Views: 238

Answers (2)

Engin
Engin

Reputation: 21

If the data is in the column "E", you can get them like:

Excel.Range bRange = xlWorkSheet.get_Range("E1","E5");

And for sorting try this:

bRange.Sort(bRange.Columns[1, Type.Missing],Excel.XlSortOrder.xlAscending);

Upvotes: 1

Ben
Ben

Reputation: 35613

This line:

Excel.Range bRange = xlWorkSheet.get_Range("A1", "P1");

Is only selecting a single row, from A1 to P1. A row is a table with one entry so sorting it doesn't do anything.

Upvotes: 1

Related Questions