MATLAB: Find last row of excel sheet in matlab using activex (including blanks)

activexexcel

Hello, I have the following excel sheet.
I want to find the last row index, which in my case should be 9.
I use the following:
nRows = objExcel.ActiveSheet.UsedRange.Rows.Count
But this gives me 6, which is the used rows excluding the blank rows. How can I get the actual range with blanks included?

Best Answer

You can do this:
% Make a reference to the very last cell in this column.
cellReference = sprintf('%s1048576', column);
Excel.Range(cellReference).Select;
currentCell = Excel.Selection;
bottomCell = currentCell.End(3); % Control-up arrow. We should be in row 1 now.
% Well we're kind of in that row but not really until we select it.
bottomRow = bottomCell.Row;
cellReference = sprintf('%s%d', column, bottomRow);
Excel.Range(cellReference).Select;
bottomCell = Excel.Selection;
bottomRow = bottomCell.Row; % This should be the last row