Finding Last Row Column
There are a number of ways to find the last row and/or column on a worksheet
0) Using Ctrl + Shift + End
1) Using the UsedRange property. (includes formatting)
2) Using SpecialCells(xlCellTypeLastCell). (includes formatting)
3) Using Cells.Find method.
4) Using End(xlUp)
Using Ctrl + Shift + End
Extends the selection from the active cell to the last used cell on the worksheet.
You will need to close and reopen the file for this to be updated.
Using the UsedRange
One of the most common ways to find the last cell on a worksheet is to use the UsedRange property.
Dim llastrow As Long
Dim llastcolumn As Long
llastrow = ActiveSheet.UsedRange.Row + ActiveSheet.UsedRange.Rows.Count - 1
Debug.Print llastrow
llastcolumn = ActiveSheet.UsedRange.Column + ActiveSheet.UsedRange.Columns.Count - 1
Debug.Print llastcolumn
When you have a "clean" worksheet the following code returns the correct last cell "E4".
However this property does not just include cells that contain actual data but also any cells that have been formatted.
For example if we run the same code on the following worksheet the last cell is "F5".
Using SpecialCells(xlCellTypeLastCell)
The same is true for the xlCellTypeLastCell argument which can be used with the SpecialCells property.
llastrow = Range("A1").SpecialCells(XlCellType.xlCellTypeLastCell).Row
Debug.Print llastrow
llastcolumn = Range("A1").SpecialCells(XlCellType.xlCellTypeLastCell).Column
Debug.Print llastcolumn
This also includes formatting in the range that is returned.
The xlLastCell constant is not documented because this constant was replaced in Excel 97 with the new xlCellType.xlCellTypeLastCell.
The following line of code does work but has only been included for backwards compatibility reasons.
llastrow = Range("A1").SpecialCells(xlLastCell).Row
The macro recorder will generate code with the xlLastCell constant which adds to the confusion.
Using Cells.Find
Another popular approach which can be used to obtain the last "populated" row and column is to use the Cells.Find method.
This method searches backwards first by row and then by column from the top left cell.
The last row is the last visible row taking Filtering into account, but is not impacted by Manually Hidden Rows or collapsed Grouping.
The last column is the last visible column taking Manually Hidden Columns into account, but is not impacted by collapsed Grouping.
llastrow = ActiveSheet.Cells.Find(What:="*", _
After:=ActiveSheet.Range("A1"), _
SearchOrder:=XlSearchOrder.xlByRows, _
SearchDirection:=XlSearchDirection.xlPrevious).Row
Debug.Print llastrow
Dim oRange As Range
Set oRange = Range("A1:E200")
llastrow = oRange.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Debug.Print llastrow
llastcolumn = ActiveSheet.Cells.Find(What:="*", _
After:=ActiveSheet.Range("A1"), _
SearchOrder:=XlSearchOrder.xlByColumns, _
SearchDirection:=XlSearchDirection.xlPrevious).Column
Debug.Print llastcolumn
llastcolumn = oRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
Debug.Print llastcolumn
Using End(xlUp)
Use the following to obtain the address of the last non-empty cell in column "A".
The last row is the last visible row taking into account Filtering, Manually Hidden Rows and collapsed Grouping.
llastrow = Range(Range("A65536").End(xlDirection.xlUp).Address).Row
llastrow = Range("A" & Rows.Count).End(xlUp).Row
Debug.Print llastrow
Checking each row or column at a time
The only way to accurately determine the next row or last row that contains data is to check every cell using ActiveCell.Offset(1,0).
Last Column in a Range
Use the following to obtain the last column in a range.
Dim oRange As Range
Set oRange = Range("A1:E200")
llastcolumn = oRange(oRange.Rows.Count, oRange.Columns.Count).Column
Debug.Print llastcolumn
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext