VBA Code to Find Last used Column or Row in Excel
Sometimes as a developer, you need to take actions in Excel sheets based on last row or column. In Excel, there are two kinds of last cells available:
- The last cell of the sheet which is XFD1048576 for Excel 2007 or above and IV65536 for Excel 2003
- The last used cell of the sheet which can also be found using Ctrl+End keys
'This function gets last cell information of the sheet
Sub GetLastCellInfo()
'Last Row of the Sheet
Sheet1.Range("E4").Value = Sheet1.Rows.Count
'Last Column of the Sheet
Sheet1.Range("E5").Value = Sheet1.Columns.Count
'Last Used Row of the Sheet
Sheet1.Range("E6").Value = Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Row
'Last Used Column of the Sheet
Sheet1.Range("E7").Value = Sheet1.Cells.SpecialCells(xlCellTypeLastCell).Column
End Sub
VBA Code to Find Last used Column or Row in Excel Steps to follow:-
- Open an Excel file
- Press Alt+F11
- Insert a Module (Insert>Module) from menu bar
- Paste the code in the module
- Now add a shape in Excel sheet
- Give a name to the shape like ‘Get Last Cell Info’
- Select ‘GetLastCellInfo’ from the list and click on ‘Ok’ button
- Select ‘GetLastCellInfo’ from the list and click on ‘Ok’ button
- Done, click on the shape to get last cell information
Download Practice File
You can also practice this through our practice files. Click on the below link to download the practice file.