Sunteți pe pagina 1din 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

Macros & VBA

VBA Tutorial: Find the Last Row, b


Column, or Cell on a Sheet 131 COMMENTS

May 11, 2015 / Jon Acampora / 131 comments

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.

Skill level: Intermediate

Video: 3 Part Series How to Find the Last Cell


with VBA

Find the Last Row, Column, or Cell in Excel VBA with the Range.End Me…
Me…

Video best viewed in full screen HD

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)

Jump to the Code Examples on this page:

Range.End VBA Code Example


Range.Find Code Example
Range.SpecialCells Code Example

Finding the Last Cell is All About the Data


Finding the last used row, column, or cell is one very common task when writing
macros and VBA applications.  Like anything in Excel and VBA, there are many di erent
ways to accomplish this.

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.

#1 – The Range.End() Method


The Range.End method is very similar to pressing the Ctrl+Arrow Key keyboard
shortcut.  In VBA we can use this method to nd the last non-blank cell in a single row
or column.

Range.End VBA Code Example

Sub Range_End_Method()
'Finds the last non-blank cell in a single row or column

Dim lRow As Long


Dim lCol As Long

'Find the last non-blank cell in column A(1)


lRow = Cells(Rows.Count, 1).End(xlUp).Row

'Find the last non-blank cell in row 1


lCol = Cells(1, Columns.Count).End(xlToLeft).Column

MsgBox "Last Row: " & lRow & vbNewLine & _


"Last Column: " & lCol

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.

Here are the help articles for Range.End

MSDN help page for Range.End


MSDN help for xlDirection Enumerations

#2 – The Range.Find() Method

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

Range.Find Code Example


The following is the code to nd the last non-blank row.

Sub Range_Find_Method()
'Finds the last non-blank cell on a sheet/range.

Dim lRow As Long


Dim lCol As Long

lRow = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row

MsgBox "Last Row: " & lRow

End Sub

Download the le that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

The Range.Find Method Explained


The Find method is looking for the rst non-blank cell (“*”).  The asterisk represents a
wildcard character that looks for any text or numbers in the cell.

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

Here is a detailed explanation for each argument.

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.

Macro Recorder to the Rescue!

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.

1. Start the macro recorder


2. Press Ctrl+F
3. Then press the Find Next button

The code for the Find method with all the arguments will be generated by the macro
recorder.

Use a Custom Function for the Find Method


You can also use a custom function (UDF) for the nd method.  Ron de Bruin's Last
Function is a perfect example.  You can copy that function into any VBA project or code
module, and use it to return the last row, column, or cell.

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.

Here are the help articles for Range.Find

MSDN Help Article for Range.Find Method

#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.

SpecialCells(xlCellTypeLastCell) Code Example

Sub Range_SpecialCells_Method()

MsgBox Range("A1").SpecialCells(xlCellTypeLastCell).Address

End Sub

Download the le that contains the code:

Find Last Cell VBA Example.xlsm (79.6 KB)

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.

Other Methods for Finding the Last Cell


Well, that should cover the basics of nding the last used or non-blank cell in a
worksheet.  If you sheet contains objects (tables, charts, pivot tables, slicers, etc.) then
you might need to use other methods to nd the last cell.  I will explain those
techniques in a separate post.

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

Do You Use VBA? PivotPal Bonus Videos: Learn


PowerQuery, Unpivoting, VBA, & More

YOU MAY ALSO LIKE

Convert Text to Time How to Write User


Values with UDFs in De ned Functions
VBA (UDFs) in Excel with
VBA

Gridlines & Freeze How to Sort Drop Down


Panes Settings Lost in Lists Automatically in
New Window – How to Excel
Fix It

131 COMMENTS « Previous 1 2


Your email address will not be published. Required elds are marked *

Comment

Name * Email * Website

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

These articles are great, they are of help understanding the


di erent approaches to address the need to nd information.
Thanks,

REPLY

Arti
March 19, 2019 at 12:01 pm

hi, i’m having a little problem can somebody help,

this is the code vba

Dim lrow As Long

lrow = Sheet1.Cells(Rows.Count, “A”).End(xlUp).Row

If Sheets(“Feuil1”).Range(“a2”) = “Canada” Then

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()

Dim lRow As Long


Dim lCol As Long

‘The 9 Find arguments are: What, After, LookIn, LookAt,


SearchOrder, SearchDirection, MatchCase, MatchByte,
SearchFormat
‘Below, I’ve ignored unneeded arguments #8 and #9 which are:
MatchByte and SearchFormat

lRow = Cells.Find(“*”, Range(“A1”), xlFormulas, xlPart, xlByRows,


xlPrevious, False).Row
lCol = Cells.Find(“*”, Range(“A1”), xlFormulas, xlPart, xlByColumns,
xlPrevious, False).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

Hello, I used the Range.Find method to nd the number of the last


column in a row, this number being used to display the a
numerical value showing how many columns the data
encompasses. It works ne normally, but when I lter is applied to
the rows the number of columns drops, even if all columns are
occupied. I would like for it to display the same number regardless
of what lters are applied, is there a way to do this?

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

In addition to the “Cons of Range.SpecialCells” section:


• Doesn’t work with protected sheets (causes the Run-time error
1004)

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

3. The cell address of 20th row and last column is————–

REPLY

moe ninja girl hack diamond


June 22, 2018 at 7:03 pm

Hello, i really think i will be back to your page

REPLY

Daniel L
February 22, 2018 at 10:44 am

I am trying to use VBA to nd rst blank cell in a speci c COLUMN


and start looking at a speci c ROW of a worksheet.
My code can nd blank in column(5) but fails because i have a
merged cell range and formula in ROW 1
I need to .PasteSpecial xlPasetValues at the point found above

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

lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address, “$”)(1)

For x = lAstRow To 1 Step -1


lAstCol = ActiveSheet.Cells(1,
ActiveSheet.Columns.Count).End(xlToLeft).Column
If lAstCol 0 Then
lAstRow = x
Exit For
End If
ActiveSheet.Rows(lAstRow).Delete
Next
‘Range(ActiveCellAddress).Select
‘MsgBox (lAstRow) & ” ” & ” Last Row”

End Sub

REPLY

Kostas Moschidis
May 1, 2018 at 11:31 am

TThe above code is not the one I published.

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

‘Kostas Moschidis – February 19, 2018


‘‘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
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

lEtterOfColumnFirstTime = Split(Cells(1, lAstCol).Address,


“$”)(1)

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

nale the problem is this

If lAstCol0 Then —- If lAstCol not equal 0 Then

because the page can not display the unequal


symbol

REPLY

rasitha
February 14, 2018 at 3:26 am

Hi,

I’m new to VBA , please someone tell me where should i start to


learn VBA , is there free sites i can go through?

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.

Range("B1:B" & lRow).FillDown

This assumes the lRow variable contains the number of


the last row, which will be set in a line a code above this.
It also assumes that your formula is in cell B1 and you
want to copy it down.

I have a bonus video at the bottom of this page that


explains how to copy and paste below the last used row.

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!

Regarding nding last row, I typically record a Macro by going to


the cell A1, and pressing CTRL+SHIFT+END. It has served me well
so far. The VBA code stays like this:

Range(“A1”).Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select

Seems pretty simple and workable.


Do you nd any disadvantages or possible errors on this method?

REPLY

a JON ACAMPORA
January 22, 2018 at 2:58 pm

Hi Johnny,

This is the Range.SpecialCells method that is explained


in method #3 above. There is a list of Pros & Cons in that
section. I hope that helps. 🙂

REPLY

Kaz
January 17, 2018 at 5:12 pm

Hi Jon, Many thanks for this article. It has helped me greatly in


preparing for my interview. Much appreciated.
Thanks,
Kaz

REPLY

a JON ACAMPORA
January 22, 2018 at 2:59 pm

Awesome! Thanks Kaz! 🙂

REPLY

Anurag Jadhav
January 12, 2018 at 1:01 am

Hello Everyone,

As now I am working on VBA code I used Cells(Rows.Count,


2).End(xlUp).Row but it show me wrong number because my excel

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

function for nd last row, more faster than ‘. nd’

P.S : Sorry for my bad English but i am french so…

Function LastRow(Optional wSheet As Worksheet) As Long



‘ cocorico

Dim i, NbCol, EndRow As Long

LastRow = 1

If Range(“B1”).Value = “” Then
NbCol = 1
Else
NbCol = Range(“A1”).End(xlToRight).Column
End If

EndRow = Cells(1, NbCol + 1).End(xlDown).Row

For i = 1 To NbCol

If Cells(EndRow, i).End(xlUp).Row > LastRow Then LastRow =


Cells(EndRow, i).End(xlUp).Row

Next i

End Function

REPLY

Srinivasan Venkatraman Srinivasan


December 4, 2017 at 10:29 pm

how to use this function.


please send sample code
i am getting type mismatch error

msgbox lastrow(“sheet1”)

or
msgbox lastrow(sheet1)

REPLY

Profex
February 12, 2018 at 11:11 am

This function assumes a lot.

It assumes that the header or data starts in row 1.


It assumes that there is no gap in headers in row 1.
(What happens if C1 is Blank, but F1 -Z1 are not?)

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)

If you have an understanding of the data that you are


expecting, there is no need to check all the columns, just
check the mandatory column(s) that you will be using to
sort/ lter the data.

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,

How to nd the address of the last column?

REPLY

Andre
November 10, 2017 at 8:03 am

Very nice example.


I wanted to nd the last active row in a sheet so I added

lastcell = Range(“A1”).SpecialCells(xlCellTypeLastCell).Address

lastrow = Range(lastcell).Row

worked like a charm

REPLY

John Mpurning
November 5, 2017 at 8:56 am

This is ne for the last used cell in a row or column; however,


another common need is to nd the last used cell in a range of a
column or row. .e.g. what is the last used cell in the
range(“A5:A10”). How does one do that?

REPLY

Bhakti
December 31, 2017 at 5:05 am

LastCell = Range(“A5”,
“A10”).SpecialCells(xlCellTypeConstants).End(xlDown).Ad
dress
MsgBox (LastCell )

if you want only row number, change .Address with .Row

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

This is just perfect but doesn’t work with VSTO :'(

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...

JOIN US & LEARN EXCEL

Learn 10 great Excel techniques


that will wow your boss and
make your co-workers say,
"how did you do that??"
Plus weekly updates to help
you learn Excel.

Download the eBook

ABOUT ME

Hello and
welcome! My
name is Jon
Acampora and
I'm here to
help you learn Excel.

This blog is updated frequently


with Excel and VBA tutorials &
tools to help improve your
Excel skills and save time with
your everyday tasks. Subscribe
above to stay updated. More
about me...

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

Charts & Dashboards

Functions & Formulas

Keyboard Shortcuts

Pivot Tables

Power Pivot

Power Query

Tables

Tips & Techniques

VBA & Macros

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

S-ar putea să vă placă și