Getting last used row/column using linq max - speed issue

Oct 23, 2013 at 9:59 PM
I've been toying with different methods to obtain the address of the last row/column containing data, as opposed to sheet.dimension.end.row or .column which may or may not be the actual range with usable data. Here's a method I thought would be pretty slick, but is actually very slow, and I can't see why. Can anyone shed light on this subject?
Private Sub GetExcelLastUsedLinqMax(ByRef sheet As ExcelWorksheet)
        Dim lastrow = sheet.Dimension.End.Row
        Dim lastcol = sheet.Dimension.End.Column
        Console.WriteLine("sheet.dimension.end.row: {0} .column {1}", lastrow, lastcol)

        Dim time As New Stopwatch
        time.Start()

        Dim querylastrow = (From cell In sheet.Cells(1, 1, lastrow, lastcol) Where cell.Value IsNot Nothing Select cell.Start.Row).Max
        Dim querylastcol = (From cell In sheet.Cells(1, 1, lastrow, lastcol) Where cell.Value IsNot Nothing Select cell.Start.Column).Max

        time.Stop()
        Console.WriteLine("Linq max query - last row {0} last col {1}, elapsed time {2}", querylastrow, querylastcol, time.ElapsedMilliseconds)
    End Sub