1

Alright so I'm trying to receive the last row of an Excel sheet. My code to do so looked like this

public static int FindLastRow(Worksheet sheet)
    {
        int lastRow;
        try
        {
            lastRow = sheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Xcl.XlSearchOrder.xlByColumns, Xcl.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;

        }
        catch (Exception)
        {
            lastRow = 1;
        }
        return lastRow;
    }

but it isn't working as needed because it's returning the wrong row. Basically it returns the row of the widest column instead of the row# of last row.

That means this one returns 7

Returns 7

and this one returns 10

Returns 10

as you can see it does not returns the last row but rather the widest. How do I return the last row of a worksheet in C#?

FlyingFoX
  • 3,379
  • 3
  • 32
  • 49
  • My bad, forgot to change the SearchOrder parameter to Rows when copying from older project – newClassSameConcept Nov 18 '19 at 12:22
  • 1
    Does this answer your question? [Programmatically getting the last filled excel row using C#](https://stackoverflow.com/questions/7674573/programmatically-getting-the-last-filled-excel-row-using-c-sharp) – HariHaran Nov 18 '19 at 12:23
  • @HariHaran I actually used the right method and everything, just forgot to change a parameter. I don't work in Interop.Excel as in your link but with the Tools version. That doesn't have the get_Range method – newClassSameConcept Nov 18 '19 at 12:25
  • @newClassSameConcept you may want to post what worked for you as an answer, then accept it - this shows the question as answered then, rather than unanswered. :) – emmademontford Nov 18 '19 at 12:42
  • @emmademontford I tried that but it's not getting accepted. Can't be arsed trying again when I'm not getting told what I did wrong in my first answer. – newClassSameConcept Nov 18 '19 at 13:18
  • @newClassSameConcept Ah, don't worry about it too much, it's most likely a bug or something. Hope you got your code working! – emmademontford Nov 18 '19 at 14:21

0 Answers0