How Find last row in excel
37 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Aitor Vitoria
el 23 de Mayo de 2016
Comentada: Image Analyst
el 24 de Mayo de 2016
Hi, I have three pages the data is in the second sheet.
In the second Sheet I would like to know which cell is the last data in column B.
from 'B2' are just numbers
Thanks
0 comentarios
Respuesta aceptada
Image Analyst
el 23 de Mayo de 2016
Editada: dpb
el 24 de Mayo de 2016
If you're comfortable with using ActiveX, here is function:
%-----------------------------------------------------------------------------
% Returns the next empty cell in column after row 1. Basically it puts the active cell in row 1
% and types control-(down arrow) to put you in the last row. Then it add 1 to get to the next available row.
function nextRow = GoToNextRowInColumn(Excel, column)
try
% 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 1
% If this cell is empty, then it's the next row.
% If this cell has something in it, then the next row is one row below it.
cellContents = Excel.ActiveCell.Value; % Get cell contents - the value (number of string that's in it).
% If the cell is empty, cellContents will be a NaN.
if isnan(cellContents)
% Row 1 is empty. Next row should be 1.
nextRow = bottomRow; % Don't add 1 since it was empty (the top row already).
else
% Row 1 is not empty. Next row should be row 2.
nextRow = bottomRow + 1; % Will add 1 to get row 1 as the next row.
end
catch ME
errorMessage = sprintf('Error in function GoToNextRowInColumn.\n\nError Message:\n%s', ME.message);
fprintf('%s\n', errorMessage);
WarnUser(errorMessage);
end
return; % from LeftAlignSheet
end % of GoToNextRowInColumn
3 comentarios
dpb
el 24 de Mayo de 2016
I try to avoid using ActiveX like the plague, but since IA hasn't been back (yet), I'll presume to take a guess that
Excel = ex;
column = 'B';
Image Analyst
el 24 de Mayo de 2016
That's correct, so it would be called like this:
GoToNextRowInColumn(Excel, 'B');
I'd recommend you use Excel rather than ex in your code - it's so much more descriptive of what it really represents than the cryptic "ex".
Más respuestas (1)
dpb
el 23 de Mayo de 2016
Not determinable unless ActiveX/COM interaction is an Excel method. In "just Matlab" best you could do would be
data=xlsread('Workbook.xls','Sheet2','B:B'); % read the column of interest
row=length(data)+1; % last row is length+1
6 comentarios
Image Analyst
el 23 de Mayo de 2016
To get the last used row you'd do
lastUsedRow = rowsn + rowst;
I created a new blank spreadsheet and put in 4 columns and 11 rows, and for some reason that I don't understand the raw cell array was not 11 rows tall, it was 1,048,576 tall even though I never put anything down there. I've attached my workbook. I'm not sure why it works differently for you. I have MATLAB R2016a and Excel 2013. If I, in Excel, type control-end, it goes to the bottom right corner of the data that is actually there, as you'd expect. I don't know why xlsread() gets a different range but they did a lot of changes to xlsread() in R2015b or R2016a.
The ActiveX way that I can think of is to do what I did, but just don't add 1 to the row, which puts it a row below.
A nifty trick for figuring out ActiveX code is to just record a macro in Excel, then edit the macro. Then transfer the macro code to your m-file.
dpb
el 23 de Mayo de 2016
Editada: dpb
el 24 de Mayo de 2016
Well, there's still something wrong with downloading files from the links here in Answers with Firefox; it won't save anything except as text so I can't get to your file, sorry. I don't know if it something TMW is doing that is, like, MS-specific for Explorer or if Firefox is broke but it's been this way since, like forever, now (although at the very firstest, I don't recall it being a problem). (+)
It probably wouldn't prove anything anyway as I'm still on this old machine with R2012b and Office 2000.
I've seen things like that with this combination, too, on occasion, however. I've no klew what causes it (and doubt that MS would, either, frankly).
As for recording macros; yeah, that is essentially the only way one can write code for Excel unless one wants to make it a career. I'll leave that to those who want to and will stick to the parts that are, at least, Matlab syntax. :)
(+) If you want, you can send the file via the contact link; I think it's visible, IA...
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!