# Excel Formula to find last row

Could someone help me with an excel formula to find the last row in column A even if the data has blanks scattered throughout?

Scott Barnes
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Excel & VBA ExpertCommented:
Formula Solution:

The following formula will return you the row of last cell with data in the range A1:A20
=MAX(INDEX((A1:A20<>"")*ROW(A1:A20),0))

VBA Solution:

The following code will return you the last row with data in col. A
Sub FindLastRowWithData_1()
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
MsgBox LR
End Sub
Older than dirtCommented:
If you remove the numbers from sktneers' formula it's more flexible.

=MAX(INDEX((A:A<>"")*ROW(A:A),0))

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Sorry for not specifying but I need to be able do this using a formula, not vba, it will be used in name manager for a pivot table.  The trick is that the data will sometimes have blanks.  If row 10 is blank and row 11 had data again, I don't want it to return that 9 is the last row.

Currently I'm using this and it doesn't work correctly when there are blanks.

=OFFSET(PivotData!\$A\$1,0,0,MAX(2,COUNTA(PivotData!\$A:\$A)),COUNTA(PivotData!\$1:\$1))

Thank you,
Older than dirtCommented:
Excel & VBA ExpertCommented:
Try this.....

=OFFSET(PivotData!\$A\$1,0,0,MAX(INDEX((PivotData!A:A<>"")*ROW(PivotData!A:A),0)),COUNTA(PivotData!\$1:\$1))
Older than dirtCommented:
@Scott: Please consider Requesting Attention so that you can split the points between sktneer and I since all I did was to modify his formula.
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.