Second to last filled cell?

My first row has X filled cells counting from A1. I want to get a reference to the second to the last cell (so if C1 is the last filled, I want B1, if U1 is the last filled I want T1).

Here’s corresponding VBA code:
Function GetSecondToLast() As Range
Set GetSecondToLast = Range(“A1”).End(xlToRight).Offset(, -1)
End Function

I know that I need to handle the special cases (A1 or B1 is empty), but I know how to do those without overly complicating this. I can assume here there’s no empty cells in between (like “A1 has value, B1 does not and C1 has value” kind of case isn’t necessarily relevant here).

So how to do this in Aspose.Cells with C#?

@artokilponen,

You may get the last cell by Row.LastDataCell. And row[row.LastDataCell.Column-1] will provide the second to the last cell(Here we assume the first row is not empty and the last cell is not A1. For those special situations, you may do the needful check and handle them accordingly.

1 Like

Thank you, that solved it.

@artokilponen,
You are welcome. I’m glad your issue has been solved. If you have any questions, please feel free to contact us.