This article will explain every method of finding last row and column in excel in easiest ways.
1. Find Last Non-Blank Row in a Column using Range.End
Let’s see the code first. I’ll explain it letter.
Sub getLastUsedRow()Dim last_row As Integerlast_row = Cells(Rows.Count, 1).End(xlUp).Row ‘This line gets the last rowDebug.Print last_rowEnd Sub
The the above sub finds the last row in column 1.
How it works?
It is just like going to last row in sheet and then pressing CTRL+UP shortcut.
Cells(Rows.Count, 1): This part selects cell in column A. Rows.Count gives 1048576, which is usually the last row in excel sheet.
Cells(1048576, 1)
Cells(Rows.Count, 1).End(xlUp)
Select the last cell with data in a column
If you want to select the last cell in A column then just remove “.row” from end and write .select.
Sub getLastUsedRow()Cells(Rows.Count, 1).End(xlUp).Select ‘This line selects the last cell in a columnEnd Sub
The “.Select” command selects the active cell.
Get last cell’s address column
If you want to get last cell’s address in A column then just remove “.row” from end and write .address.
Sub getLastUsedRow()add=Cells(Rows.Count, 1).End(xlUp).address ‘This line selects the last cell in a columnDebug.print addEnd Sub
The Range.Address function returns the activecell’s address.
Find Last Non-Blank Column in a Row
It is almost same as finding last non blank cell in a column. Here we are getting column number of last cell with data in row 4.
Sub getLastUsedCol()Dim last_col As Integerlast_col = Cells(4,Columns.Count).End(xlToLeft).Column ‘This line gets the last columnDebug.Print last_colEnd Sub
You can see in image that it is returning last non blank cell’s column number in row 4. Which is 4.
How it works?
Well, the mechanics is same as finding last cell with data in a column. We just have used keywords related to columns.
Select Data Set in Excel Using VBA
Now we know, how to get last row and last column of excel using VBA. Using that we can select a table or dataset easily. After selecting data set or table, we can do several operations on them, like copy-paste, formating, deleting etc.
Here we have data set. This data can expand downwards. Only the starting cell is fixed, which is B4. The last row and column is not fixed. We need to select the whole table dynamically using vba.
VBA code to select table with blank cells
Sub select_table()Dim last_row, last_col As Long'Get last rowlast_row = Cells(Rows.Count, 2).End(xlUp).Row'Get last columnlast_col = Cells(4, Columns.Count).End(xlToLeft).Column'Select entire tableRange(Cells(4, 2), Cells(last_row, last_col)).SelectEnd Sub
When you run this, entire table will be selected in fraction of a second. You can add new rows and columns. It will always select the entire data.
Benefits of this method:
- It’s easy. We literally wrote only one line to get last row with data. This makes it easy.
- Fast. Less line of code, less time taken.
- Easy to understand.
- Works perfectly if you have clumsy data table with fixed starting point.
- The starting point must be know.
- You can only get last non-blank cell in a known row or column. When your starting point is not fixed, it will be useless. Which is very less likely to happen.
2. Find Last Row Using Find() Function
Let’s see the code first.
Sub last_row()lastRow = ActiveSheet.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).RowDebug.Print lastRowEnd Sub
As you can see that in image, that this code returns the last row accurately.
How it works?
Here we use find function to find any cell that contains any thing using wild card operator "*". Asterisk is used to find anything, text or number.
We set search order by rows (searchorder:=xlByRows). We also tell excel vba the direction of search as xlPrevious (searchdirection:=xlPrevious). It makes find function to search from end of the sheet, row wise.Once it find a cell that contains anything, it stops. We use the Range.Row method to fetch last row from active cell.
- You don’t need to know the starting point. It just gets you last row.
- It can be generic and can be used to find last cell with data in any sheet without any changes.
- Can be used to find any last instance of specific text or number on sheet.
- It is ugly. Too many arguments.
- It is slow.
- Can’t use to get last non blank column. Technically, you can. But it gets too slow.
3. Using SpecialCells Function To Get Last Row
The SpecialCells function with xlCellTypeLastCell argument returns the last used cell. Lets see the code first
Sub spl_last_row_()lastRow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).RowDebug.Print lastRowEnd Sub
If you run the above code, you will get row number of last used cell.
Sub Macro1()'' Macro1 Macro''ActiveCell.SpecialCells(xlLastCell).SelectEnd Sub
ليست هناك تعليقات:
إرسال تعليق