VBA Find Last Used Row with data in particular Column – Excel Macros Examples Codes helps in automation. There are certain situations where we perform some tasks by finding last used Row with data in a Column. For examples, There may be many columns with data and each column may have different number of items (rows). In this situation we need to find exact number of rows in a specific column to avoid the unnecessary looping of all rows even if there is no data.
Solution(s):
We can count the number of rows in the active sheet from there we can come back in particular row to get the exact number of rows with data. We can use Row property to get last used Row.
Finding last used Row with data in particular Column – Example
The following example will show you how to find last Row with data in a particular Column. In this example we are finding the last used Row in Column A.
Sub sbLastRowOfAColumn() 'Find the last Row with data in a Column 'In this example we are finding the last row of column A Dim lastRow As Long With ActiveSheet lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row End With MsgBox lastRow End Sub
Instructions:
- Open an excel workbook
- Press Alt+F11 to open VBA Editor
- Insert a New Module from Insert Menu
- Copy the above code and Paste in the code window
- To check, enter some data in a first Column at A15
- Now Run a Macro, you should see a message box with result 15
Output:
Here is the example data and screen-shot with output to show how Finding last used Row with data in particular Column works.
Finding last used Row with data in particular Column- Case Study
The following example will show you real time example and when need find the last Row with data in each Column.
Requirement:
- User wants to paste his data for 5 Departments – one department data in one row
- Number of items in each Column could be any thing between 1 to 16(it is not fixed in all the Columns)
- Data may not be available for all Departments
- Find maxim sales from 5 departments and Bold the Font/li>
Code:
Sub sbLastRowOfAColumnExamples() Dim lastRow, lRow As Integer Dim iCntr, jCntr, iMaxRow As Integer Dim vMax For iCntr = 1 To 5 ' for each column vMax = 0 iMaxRow = 2 'Finding last Row of current Column With ActiveSheet lastRow = .Cells(.Rows.Count, iCntr).End(xlUp).Row End With lRow = lastRow Range(Cells(2, iCntr), Cells(lRow, iCntr)).Font.Bold = False For jCntr = 2 To lRow If vMax < Cells(jCntr, iCntr) Then vMax = Cells(jCntr, iCntr) iMaxRow = jCntr End If Next Cells(iMaxRow, iCntr).Font.Bold = True Next End Sub
Instructions:
Download the file below and Click on the FindMax button and Explore the Code.
Output
Download Example File
Download the example file and Explore the use of Finding last used Row with data in particular Column.
Download Now :ANALYSISTABS -Last Row with Data in a Column
Range(“A:A”).SpecialCells(xlCellTypeLastCell).Select
‘to select last cell from column “A”, same code can be used to find last cell from row.
Feedback! Waiting…..
Hi Ram,
This works fine if your data increases always.
Try this case:
-> Add a new worksheet.
-> Enter some value at Range A10 and A15.
===========> You code give the correct result as 15.
-> Now delete data at A15
->Now run you code, again it will return 15, instead of 10.
So, you code always return last used row number (even if no data).
————
Example show in this topic will return the correct, last row number with data.
Hope this clarifies your query.
Thanks-PNRao!
Happy morning Mr. PN Rao,
If I’m not wrong, the following code will also gives the above same results, without predefine column number. .
Let me know, are all of your course is available free of cost or for any payment
Any better ideas or modifications are highly appreciable.
sai babu
Good morning Mr. Sai!
Yes, there are many ways to solve the same problems. The code which we are providing here to help both beginners and Advanced users.
And regarding the courses, there are lot of topics available in our blog for a free reference for VBA developers. The online courses which we are going to start will cost 60 to 100 USD (3500/- to 6000/-). We will send you the exact pricing in couple of weeks.
Thanks-PNRao!
How can we do it if we are searching from a table (listobject)? When I tried the code, it always goes to the bottom of the table that is not part of the table. Example, I defined the table as A1:B10. It has data until B5 only. So the unused cell in column B should be B6 – but it goes to end of table. Could you please help? :)
try this code
Option Explicit
Sub find_last_row()
Dim My_Col, My_Ro, i As Long
Dim My_Max As Byte
My_Max = 0
My_Col = ActiveSheet.Cells(1, Columns.Count).End(1).Column
For i = 1 To My_Col
My_Ro = Cells(Rows.Count, i).End(3).Row
If My_Max < My_Ro Then My_Max = My_Ro
Next
MsgBox "The laste used row is:" & My_Max
End Sub
Hi i want to value only the cells of a particular column that has values in it and exclude the cells that has only formulas