8

I have a Microsoft.Office.Interop.Excel.Range object and want to estimate the end-coordinates of that range ie (last column, last row).

There are no properties like LastCol, LastRow. The only properties are Column and Row, which specify the first cell. But how can I get the last cell of the range?

2
  • 1
    Columns.Count, Rows.Count Commented Jul 2, 2013 at 16:08
  • 1
    Columns.Count and Rows.Count will work for a contiguous Range. For a non-contiguous range (e.g. "D1:E3,A1:B3") you will need to use the Areas property. Commented Jul 2, 2013 at 19:13

3 Answers 3

10

there is two way to do this :

  1. Using Worksheet.UsedRange to determine the range. This will give you a range like A1:F10, or use Worksheet.UsedRange.SpecialCells(IExcel.XlCellType.xlCellTypeLastCell) to obtain F10.

  2. With Range.End[IExcel.XlDirection] property, it returns the last continuous no-empty cell in the specified direction. Note that if the Range is empty, it will return the first no-empty cell or last cell(when excel reach its boundaries) in the given direction. Ex: the whole column A is empty, Range["A1"].End[IExcel.XlDirection.xlDown] will be A65535(last row in excel 97-2003, A1048576 for excel 2007)

//Just In case if you are wondering what IExcel is
using IExcel = Microsoft.Office.Interop.Excel;

Sign up to request clarification or add additional context in comments.

Comments

9

This should get the first first and last cell of the range:

  1. Initiate Excel.

  2. Open workbook.

  3. Select your range, in this case I got the used range of the active sheet.

  4. Call the get_address method of the range.

  5. Split the result of get_address on the colon.

  6. The first value of the array resulting from the split will have the beginning cell.

  7. The second value of the array resulting from the split will have the ending cell.

  8. Replace the dollar signs with nothing and you will have the beginning and ending cells for the range.

    Microsoft.Office.Interop.Excel.Application excel = new Application();
    Microsoft.Office.Interop.Excel.Workbook workBook =
        excel.Workbooks.Open(fileLocation);
    Microsoft.Office.Interop.Excel.Worksheet sheet = workBook.ActiveSheet;
    Microsoft.Office.Interop.Excel.Range range = sheet.UsedRange;
    string address = range.get_Address();
    string[] cells = address.Split(new char[] {':'});
    string beginCell = cells[0].Replace("$", "");
    string endCell = cells[1].Replace("$", "");
    workBook.Close(true);
    excel.Quit();
    

If you want the last column and last row relative to the beginning of the range you can do the following:

    int lastColumn = range.Columns.Count;
    int lastRow = range.Rows.Count;

2 Comments

I hope you're still active, how can i get the specific alphabet of the last column? not the integer. here's a pseudo code.... Range cell = sheet.Range["A1:"+ string_lastColumn + Int_lastColumn];
to answer my own question, and for someone who also handle dynamic columns, where doing modulo looping, in alphabetical order, 26 is equal to letter "Z". when your last column is 27, it's equal to letter "AA", 28=AB, 29=AC... we're going to create a massive loop for this... Comment if there's a better way of doing this.
0

Using Excel 2013, we've had lots of cases where UsedRange and xlCellTypeLastCell simply gave horribly wrong values.

For example, we'd have a worksheet containing just 7 columns, and sometimes these two functions would tell our C# code that there were 16,000+ columns of data.

The only method I found which truly worked out the bottom-right cell containing data was to use the tips in this suggestion:

sheet.Cells.Find()

1 Comment

I've encountered this too. The values appear to be way off because .SpecialCells(XlCellType.xlCellTypeLastCell) returns the last 'used' cell of the sheet object's UsedRange property, not the last cell of the Range object it's called on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.