Getting Last Non-Blank Row Number With VBA

A

Andy

Hi Everyone

In Excel I can place the following in a cell to get the last non blank
row number in column A. This works great and it's super quick.

=ROW(OFFSET(A1,COUNTA(A:A)-1,0))

To do the same with VBA code seems very complex. Can you please show
me some VBA code that will give me the last non-blank row number for a
given column?

Andy
 
K

Ken

Andy

What your formula actually does is count how many non blanks there are
in column A, and give the row number of the cell that is offset one
less than that number from cell A1; which, if there are no blanks
cells in column A turns out to be the last non blank row number. If
there are any blanks, it won't give the number you want. To replicate
the functionality you described in VBA is pretty easy. All you have
to do is go to the bottom of the spreadsheet, end up to the last non
blank row with something in it. You don't say what you want to do
with the number, but

Sub Macro2()
MsgBox Cells(Rows.Count, 1).End(xlUp).Row
End Sub

will tell you what the number is.

I hope this helps.

Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top