This will work regardless of the version of Excel (2003, 2007, 2010). The first has 65,536 rows per sheet, and the last two have a million rows or so. Sheet1.Rows.Count returns this number depending on the version.
numofrows = Sheet1.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp).Row
or equivalent but shorter
numofrows = Sheet1.Cells(Sheet1.Rows.Count,1).End(xlUp)
This looks for the bottom of column A for the first non-empty cell and gets the row number.
This also works if you have data that goes further in other columns. So, for example, if you take the data of your example and also write something in cell FY4763, the above will still correctly return 9 (not 4763 that any method that includes the UsedRange property will be incorrectly returned).
Note that indeed, if you need a cell reference, you should simply use the following. You do not need to get the line number first and then create a link to the cell.
Set rngLastCell = Sheet1.Range("A1").Offset(Sheet1.Rows.Count - 1, 0).End(xlUp)
Please note that this method does not work in some cases:
- The last line contains data
- Last lines (lines) are hidden or filtered
So stay tuned for whether you plan to use line 1,048,576 for these things!
Jean-Franรงois Corbett Jun 10 2018-11-11T00: 00Z
source share