How can I find the last row that contains data in a specific column and on a specific sheet?
相关问题
- Excel sunburst chart: Some labels missing
- Error handling only works once
- Error handling only works once
- Excel formula in VBA code
- Excel VBA run time error 450 from referencing a ra
相关文章
- Get column data by Column name and sheet name
- programmatically excel cells to be auto fit width
- Unregister a XLL in Excel (VBA)
- Unregister a XLL in Excel (VBA)
- How to prevent excel from truncating numbers in a
- numeric up down control in vba
- Declare a Range relative to the Active Cell with V
- What's the easiest way to create an Excel tabl
Simple and quick:
Example use:
I would like to add one more reliable way using
UsedRange
to find the last used row:Similarly to find the last used column you can see this
Result in Immediate Window:
All the solutions relying on built-in behaviors (like
.Find
and.End
) have limitations that are not well-documented (see my other answer for details).I needed something that:
Worksheet_Change
handler without feeling sluggish)The solution below:
UsedRange
to find the upper bound for the row number (to make the search for the true "last row" fast in the common case where it's close to the end of the used range);UsedRange
we need to skip)(No tests, sorry)
sheet.UsedRange.Rows.Count: retrurn number of rows used, not include empty row above the first row used
if row 1 is empty, and the last used row is 10, UsedRange.Rows.Count will return 9, not 10.
This function calculate the first row number of UsedRange plus number of UsedRange rows.
You should use the
.End(xlup)
but instead of using 65536 you might want to use:That way it works for Excel 2007 which I believe has more than 65536 rows