Professional Documents
Culture Documents
VBA Tutorial - Find The Last Row, Column, or Cell in Excel
VBA Tutorial - Find The Last Row, Column, or Cell in Excel
VBA Tutorial - Find The Last Row, Column, or Cell in Excel
x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
Bottom line: Learn how to nd the last row, column, or cell in a worksheet using three
di erent VBA methods. The method used depends on the layout of your data, and if
the sheet contains blank cells.
Find the Last Row, Column, or Cell in Excel VBA with the Range.End Me…
Me…
https://www.excelcampus.com/vba/find-last-row-column-cell/ 1/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Download the le that contains the code: x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
Find Last Cell VBA Example.xlsm (79.6 KB)
Choosing the right method mostly depends on what your data looks like.
In this article I explain three di erent VBA methods of the Range object that we can use
to nd the last cell in a worksheet. Each of these methods has pros and cons, and
some look scarier than others. 🙂
But understanding how each method works will help you know when to use them, and
why.
Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column
End Sub
https://www.excelcampus.com/vba/find-last-row-column-cell/ 2/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Download the le that contains the code: x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
Find Last Cell VBA Example.xlsm (79.6 KB)
To nd the last used row in a column, this technique starts at the last cell in the column
and goes up (xlUp) until it nds the rst non-blank cell.
The Rows.Count statement returns a count of all the rows in the worksheet.
Therefore, we are basically specifying the last cell in column A of the sheet (cell
A1048567), and going up until we nd the rst non-blank cell.
It works the same with nding the last column. It starts at the last column in a row,
then goes to the left until the last non-blank cell is found in the column.
Columns.Count returns the total number of columns in the sheet. So we start at the
last column and go left.
The argument for the End method speci es which direction to go. The options are:
xlDown, xlUp, xlToLeft, xlToRight.
Pros of Range.End
Range.End is simple to use and understand since it works the same way as the
Ctrl+Arrow Key shortcuts.
Can be used to nd the rst blank cell, or the last non-blank cell in a single row or
column.
Cons of Range.End
Range.End only works on a single row or column. If you have a range of data that
contains blanks in the last row or column, then it may be di cult to determine which
row or column to perform the method on.
If you want to nd the last used cell then you have to evaluate at least two
statements. One to nd the last row and one to nd the last column. You can then
combine these to reference the last cell.
The Range.Find method is my preferred way to nd the last row, column, or cell. It is
the most versatile, but also the scariest looking. 🙂
Range.Find has a lot of arguments, but don't let this scare you. Once you know what
they do you can use Range.Find for a lot of things in VBA.
Range.Find is basically the way to program the Find menu in Excel. It does the same
thing, and most of the arguments of Range.Find are the options on the Find menu.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 3/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
Sub Range_Find_Method()
'Finds the last non-blank cell on a sheet/range.
lRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
End Sub
Starting in cell A1, it moves backwards (xlPrevious) and actually starts it's search in the
very last cell in the worksheet. It then moves right-to-left (xlByRows) and loops up
through each row until it nds a non-blank cell. When a non-blank is found it stops and
returns the row number.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 4/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
What:=”*” – The asterisk is a wildcard character that nds any text or number in the
cell. It's basically the same as searching for a non-blank cell.
After:=Range(“A1”) – Start the search after cell A1, the rst cell in the sheet. This
means that A1 will NOT be searched. It will start the search after A1 and the next cell
it searches depends on the SearchOrder and SearchDirection. This argument can be
changed to start in a di erent cell, just remember that the search actually starts in
the cell after the one speci ed.
LookAt:=xlPart – This is going to look at any part of the text inside the cell. The
other option is xlWhole, which would try to match the entire cell contents.
LookIn:=xlFormulas – This tells Find to look in the formulas, and it is an important
argument. The other option is xlValues, which would only search the values. If you
have formulas that are returning blanks (=IF(A2>5,”Ok”,””) then you might want to
consider this a non-blank cell. Specifying the LookIn as xlFormulas will consider this
formula as non-blank, even if the value returned is blank.
SearchOrder:=xlByRows – This tells Find to search through each entire row before
moving on to the next. The direction is searches left-to-right or right-to-left depends
on the SearchDirection argument. The other option here is xlByColumns, which is
used when nding the last column.
SearchDirection:=xlPrevious – This speci es which direction to search. xlPrevious
means it will search from right-to-left or bottom-to-top. The other option is xlNext,
which moves in the opposite direction.
MatchCase:=False – This tells Find not to consider upper or lower case letters.
Setting it to True would consider the case. This argument isn't necessary for this
scenario.
Ok, I know that's a lot to read, but hopefully you will have a better understanding of
how to use these arguments to nd anything in a worksheet.
Pros of Range.Find
Range.Find searches an entire range for the last non-blank row or column. It is NOT
limited to a single row or column.
The last row in a data set can contain blanks and Range.Find will still nd the last
row.
The arguments can be used to search in di erent directions and for speci c values,
not just blank cells.
Cons of Range.Find
It's ugly. The method contains 9 arguments. Although only one of these arguments
(What) is required, you should get in a habit of using at least the rst 7 arguments.
Otherwise, the Range.Find method will default to your last used settings in the Find
window. This is important. If you don't specify the optional arguments for LookAt,
LookIn, and SearchOrder then the Find method will use whatever options you used
last in Excel's Find Window.
Finding the last cell requires two statements. One to nd the last row and one to
nd the last column. You then have to combine these to nd the last cell.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 5/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Range.Find is still my preferred method for nding the last cell because of it's x
j f
Free Webinar: The 5 Secrets
versatility. But it is to
a lotUnderstanding Pivot
to type and remember. Tables
Fortunately, you don't have to. Your Spot
Claim
You can use the macro recorder to quickly create the code with all the arguments.
The code for the Find method with all the arguments will be generated by the macro
recorder.
I also have a similar function in the example workbook. My function just has additional
arguments to reference the worksheet and range to search in.
#3 – Range.SpecialCells(xlCellTypeLastCell)
The SpecialCells method does the same thing as pressing the Ctrl+End keyboard
shortcut, and selects the last used cell on the sheet.
Sub Range_SpecialCells_Method()
MsgBox Range("A1").SpecialCells(xlCellTypeLastCell).Address
End Sub
It's actually the easiest way to nd the last used cell. However, this method is nding
the last used cell, which can be di erent than the last non-blank cell.
Often times you will hit Ctrl+End on the keyboard and be taken to some cell way down
at the end of the sheet that is de nitely not used. This can occur for a number of
reasons. One common reason is the formatting properties for that cell have been
changed. Simply changing the font size or ll color of a cell will ag it as a used cell.
Pros of Range.SpecialCells
You can use this method to nd “used” rows and columns at the end of a worksheet
and delete them. Comparing the result of Range.SpecialCells with the result of the
https://www.excelcampus.com/vba/find-last-row-column-cell/ 6/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Range.Find for non-blanks can allow you to quickly determine if any unused rows or x
j f
Free Webinar: Thecolumns
5 Secrets to Understanding Pivot Tables
exist on the sheet. Claim Your Spot
Deleting unused rows/columns can reduce le size and make your scroll bar bigger.
Cons of Range.SpecialCells
Excel only resets the last cell when the workbook is saved. So if the user or
macro deletes the contents of some cells, then this method will not nd the true last
cell until after the le is saved.
It nds the last used cell and NOT the last non-blank cell.
I also have an article on how to nd the FIRST used cell in the worksheet.
Please leave a comment below if you have any questions, or are still having trouble
nding the last cell. I'll be happy to help! 🙂
PREVIOUS NEXT
Comment
SUBMIT COMMENT
Nanzy
May 28, 2019 at 3:14 pm
https://www.excelcampus.com/vba/find-last-row-column-cell/ 7/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Hi, thanks for the article. x
j f
Free Webinar: The 5 Secrets to Understanding
I wanted to ask that if I had toPivot Tables
get the value of the last non-blank
Claim Your Spot
cell and store it to a variable then how can I do it? That means I just
want the value that the cell contains.
REPLY
Luis G Polanco
April 15, 2019 at 9:50 pm
REPLY
Arti
March 19, 2019 at 12:01 pm
Range(“b2”).Value = (“CAN”)
Else
Range(“b2”).Value = (“INT”)
End If
End Sub
it doesn’t scroll down the if statement but it works for only cell b2
but not for b3.b4.b5.etc…
thank you!
REPLY
yasser
February 2, 2019 at 4:49 am
Help
Sum each end page by page in a particular cell and column by VBA
REPLY
Wayne Edmondson
December 6, 2018 at 10:04 pm
Hi Jon,
Just thought I would share this with you and other readers (see sub
below). I’ve used your Cells.Find() method with great success, but
as you mentioned, it is a lot to type out and set up. It dawned on
me to collapse it into a single line for each by using the “,”
argument method vs. the “Argument:=” method. As you mentioned
in your article, you can ignore the last two, as MatchByte is not
relevant except for obscure language characters and unless you
are nding cell or font formats, then SearchFormat can be ignored.
At rst, I thought I would need two comma placeholders at the end
of the argument list (for MatchByte and SearchFormat), but I
discovered it works without them.. probably because they are in
succession and at the end of the list. De ning the lRow and lCol in
this way makes it pretty easy to write on the y and does not
vertically stack up your code too much. Once you write it out a few
https://www.excelcampus.com/vba/find-last-row-column-cell/ 8/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
times, it is pretty each to remember and you need change only two x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
items to go from row to column. As a reminder, I commented theYour Spot
Claim
reference to the nine Arguments, just as a ticker to the names and
the sequence in which they must appear if using only commas. Of
course, there is nothing wrong with the more stacked “Argument:=”
method.. just more bulk and so more cumbersome and or
intimidating to use. Putting it all in one line makes it not that much
more cumbersome than typing say: lRow = Cells(Row.Count,
1).End(xlUp).Row, but you get the bene t of the returned lRow
being for the entire worksheet vs. just one column of the
worksheet. Anyway, I thought I would share this di erent
perspective. I hope others nd it helpful. Thanks for the great tips
and the inspiration to look at things in di erent ways. Thumbs up!
Cheers!
Wayne Edmondson
Sub Find_Last_Used_Row_And_Column()
MsgBox “Last Row: ” & lRow & vbCrLf & vbCrLf & “Last Column: ” &
lCol & vbCrLf & vbCrLf & “Cell address: ” & Cells(lRow, lCol).Address
End Sub
REPLY
Chris
August 13, 2018 at 9:58 am
REPLY
Lino Franculli
August 21, 2018 at 4:30 pm
Thanks a bunch for sharing this with all folks you really
realize what you are speaking about! Bookmarked.
Please also visit my site =). We could have a hyperlink
trade agreement among us
REPLY
Will Roberts
June 28, 2018 at 11:59 am
How do you nd the last cell in a print range, whether used or not
used? What if a worksheet has multiple print ranges? How do I go
from the rst to the next.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 9/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
I would like to navigate to the last cell in the print range and adjust x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
the row height and column width to ll the page to the margins.
Claim Your Spot
Forcing the worksheet to t to print to one page is not what I want.
REPLY
Mika
June 28, 2018 at 2:52 am
REPLY
Marin Draganov
June 23, 2018 at 6:20 am
There is a tiny di erence between the rst and the second method:
to get the same result in the second case you need to activate the
sheet to be seached:
Sheet2.Activate
When I don’t use the above line I get quite a wrong result using
Range.Find 😉
REPLY
bijoy halder
March 2, 2018 at 2:33 am
REPLY
REPLY
Daniel L
February 22, 2018 at 10:44 am
REPLY
Kostas Moschidis
February 19, 2018 at 10:58 pm
‘To solve the problem with the blank lines and columns we can use
the ‘following code.
‘This code deletes blank columns and rows, from the end, to clear,
release, ‘sheet and memory
‘Also, if excel had many empty rows and columns, after
implementing the code, ‘it is getting faster !
Sub rEalLastColumn()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
https://www.excelcampus.com/vba/find-last-row-column-cell/ 10/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Dim wS As Worksheet x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
Dim lEtterOfColumn As String Claim Your Spot
Dim lEtterOfColumnFirstTime As String
Dim ActiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer
ActiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange =
wS.UsedRange.Cells(wS.UsedRange.Rows.Count,
wS.UsedRange.Columns.Count)
lAstCol = lAstCellUsedRange.Column
lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address, “$”)(1)
For x = lAstCol To 1 Step -1
lEtterOfColumn = Split(Cells(1, x).Address, “$”)(1)
lAstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count,
lEtterOfColumn).End(xlUp).Row
If lAstRow 0 Then
lAstCol = x
Exit For
End If
ActiveSheet.Columns(lEtterOfColumn).Delete
Next
‘Range(ActiveCellAddress).Select
‘MsgBox (lAstCol) & ” ” & lEtterOfColumn & ” Last Column ”
End Sub
Sub rEalLastRowAndDeleteEmptyRowsToClearSheetAndMemory()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
Dim wS As Worksheet
Dim lEtterOfColumn As String
Dim lEtterOfColumnFirstTime As String
Dim ActiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer
Dim lEtterOfRow As String
ActiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange =
wS.UsedRange.Cells(wS.UsedRange.Rows.Count,
wS.UsedRange.Columns.Count)
lAstCol = lAstCellUsedRange.Column
lAstRow = lAstCellUsedRange.Row
End Sub
REPLY
Kostas Moschidis
May 1, 2018 at 11:31 am
https://www.excelcampus.com/vba/find-last-row-column-cell/ 11/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
I do not understand how it changed. x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
The follower codes is the right onehis is not myClaim
codes..Your Spot
My codes are that following
Sub
rEal_LastCol_AndDeleteEmptyColumnsToClearSheetAnd
Memory()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
Dim wS As Worksheet
Dim lEtterOfColumn As String
Dim lEtterOfColumnFirstTime As String
Dim aCtiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer
aCtiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange =
wS.UsedRange.Cells(wS.UsedRange.Rows.Count,
wS.UsedRange.Columns.Count)
lAstCol = lAstCellUsedRange.Column
lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address,
“$”)(1)
For x = lAstCol To 1 Step -1
lEtterOfColumn = Split(Cells(1, x).Address, “$”)(1)
lAstRow = ActiveSheet.Cells(ActiveSheet.Rows.Count,
lEtterOfColumn).End(xlUp).Row
If lAstRow 0 Then
lAstCol = x
Exit For
End If
ActiveSheet.Columns(lEtterOfColumn).Delete
Next
MsgBox (lAstCol) & “=” & ” lAstCol”
End Sub
Sub
rEal_LastRow_AndDeleteEmptyRowsToClearSheetAndMe
mory()
Dim lAstCol As Long
Dim lAstCellUsedRange As Range
Dim wS As Worksheet
Dim lEtterOfColumn As String
Dim lEtterOfColumnFirstTime As String
Dim aCtiveCellAddress As String
Dim x As Integer
Dim lAstRow As Integer
Dim lEtterOfRow As String
aCtiveCellAddress = ActiveCell.Address
Set wS = ActiveSheet
Set lAstCellUsedRange =
wS.UsedRange.Cells(wS.UsedRange.Rows.Count,
wS.UsedRange.Columns.Count)
lAstCol = lAstCellUsedRange.Column
lAstRow = lAstCellUsedRange.Row
https://www.excelcampus.com/vba/find-last-row-column-cell/ 12/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
For x = lAstRow To 1 Step -1 x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
lAstCol = ActiveSheet.Cells(1, Claim Your Spot
ActiveSheet.Columns.Count).End(xlToLeft).Column
If lAstCol 0 Then
lAstRow = x
Exit For
End If
ActiveSheet.Rows(lAstRow).Delete
Next
MsgBox (lAstRow) & “=” & ” Last Row”
End Sub
REPLY
Kostas Moschidis
May 1, 2018 at 11:41 am
REPLY
rasitha
February 14, 2018 at 3:26 am
Hi,
Thnak you
REPLY
James
January 30, 2018 at 9:18 am
Hi Jon,
What would be the best way to nd AND select the last last row in
a range? I am attempting to enter a formula in the rst row of a
range and copy it down to the last row in the range?
Thanks!
REPLY
a JON ACAMPORA
January 31, 2018 at 9:32 am
Hi James,
You can use a variable to store the value of the last row,
then use in the range reference. There are many ways to
do this, but something like the following will work.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 13/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Some of the techniques in that video might help you as x
j f
Free Webinar: The 5 Secrets to Understanding
well. Pivot Tables Claim Your Spot
REPLY
Will
January 24, 2018 at 4:56 pm
What is the proper way to use these functions to when you have
multiple workbooks and worksheets? We just updated from 2013
to 2016 and a lot of our projects broke due to these functions
accessing di erent workbooks under one version than the other.
REPLY
Johnny
January 20, 2018 at 6:22 am
Hi Jon!
Your tips are always great, specially when it comes about
shortcuts! Thank you for all!
Range(“A1”).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
REPLY
a JON ACAMPORA
January 22, 2018 at 2:58 pm
Hi Johnny,
REPLY
Kaz
January 17, 2018 at 5:12 pm
REPLY
a JON ACAMPORA
January 22, 2018 at 2:59 pm
REPLY
Anurag Jadhav
January 12, 2018 at 1:01 am
Hello Everyone,
https://www.excelcampus.com/vba/find-last-row-column-cell/ 14/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
sheet having Table style so it showing last cell of this table even it’s x
j f
Free Webinar: The 5 Secrets toPlease
blank. Understanding Pivot
help me to search Tables
non blank cell even excel Claim
havingYour Spot
Table style.
Thanks.
REPLY
Cocorico
November 29, 2017 at 8:07 am
LastRow = 1
If Range(“B1”).Value = “” Then
NbCol = 1
Else
NbCol = Range(“A1”).End(xlToRight).Column
End If
For i = 1 To NbCol
Next i
End Function
REPLY
msgbox lastrow(“sheet1”)
or
msgbox lastrow(sheet1)
REPLY
Profex
February 12, 2018 at 11:11 am
Why not use the same method used for nding the last
row to nd the last column? (Because it would take just
as long, or longer then the Range.Find method)
https://www.excelcampus.com/vba/find-last-row-column-cell/ 15/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Either way, if you have Filtered data, none of the x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables
methods will work properly. Claim Your Spot
REPLY
Sam
November 20, 2017 at 12:21 am
Hi,
REPLY
Andre
November 10, 2017 at 8:03 am
lastcell = Range(“A1”).SpecialCells(xlCellTypeLastCell).Address
lastrow = Range(lastcell).Row
REPLY
John Mpurning
November 5, 2017 at 8:56 am
REPLY
Bhakti
December 31, 2017 at 5:05 am
LastCell = Range(“A5”,
“A10”).SpecialCells(xlCellTypeConstants).End(xlDown).Ad
dress
MsgBox (LastCell )
REPLY
dharsh
October 29, 2017 at 11:32 pm
Hi can you please tell me that is the error in the below syntex
Sub logic_17a()
ThisWorkbook.Activate
Worksheets(“logic17a”).Select
LRow = Cells(ActiveSheet.Rows.Count, 1).End(xlsm).Row
x = Sum(Range(Cells(2, 4).Cells(LR, 4)))
Cells(LRow + 1, 4).Value = x
End Sub
REPLY
TG
October 12, 2017 at 7:05 am
https://www.excelcampus.com/vba/find-last-row-column-cell/ 16/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
Can you help to transpose this VBA code to VSTO ? x
j f
Free Webinar: The 5 Secrets to Understanding
THANK YOU IN ADVANCE! Pivot Tables Claim Your Spot
REPLY
Aaliyah
August 27, 2017 at 11:27 am
hi good evening
I am trying to get some advice on a task that I’m trying to complete
I have a excel sheet which contains columns from A to J
What i want to do is cut the information under F-J
And place them below the data that is already in A-E
So I’m thinking there has to be some nd list row rst them paste
to last row
Can you assist
REPLY
Search here...
ABOUT ME
Hello and
welcome! My
name is Jon
Acampora and
I'm here to
help you learn Excel.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 17/18
6/21/2019 VBA Tutorial: Find the Last Row, Column, or Cell in Excel
x
j f
Free Webinar: The 5 Secrets to Understanding Pivot Tables Claim Your Spot
BLOG CATEGORIES
Add-ins
Keyboard Shortcuts
Pivot Tables
Power Pivot
Power Query
Tables
TUTORIALS
COURSES
ADD-INS
DOWNLOADS
ABOUT
CONTACT
MEMBER LOGIN
© 20 19 E XC EL CA M PUS . A LL R IG HT S R ES ER VE D.
https://www.excelcampus.com/vba/find-last-row-column-cell/ 18/18