Link to home
Start Free TrialLog in
Avatar of tesla764
tesla764

asked on

excel vba

Does anyone have a good example of how to determine the number of used rows and columns in a spreadsheet?
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

I copied this a long time ago from the web

LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row
which is not very exact, because Excel doesn't keep track of the last cell in a very adequate form.
Another method to find the last used row in a particular column is:

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

but this doesn't tell you FOR SURE the last used row in the entire sheet, unless you can be certain that Column A holds the data.
A couple extra methods are more reliable.
LastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row¿or¿LastRow = ActiveSheet.UsedRange.Rows.Count
Last Column
lngLastColumn = Cells.Find("*", SearchOrder:=xlByColumns, LookIn:=xlValues, SearchDirection:=xlPrevious).Column
Avatar of tesla764
tesla764

ASKER

What is the symbol "¿" that you mentioned?
That was an error when I copy/pasted.

This

LastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row¿or¿LastRow = ActiveSheet.UsedRange.Rows.Count

should have been
LastRow = Cells.Find("*",SearchOrder:=xlByRows,SearchDirection:=xlPrevious).Row

or

LastRow = ActiveSheet.UsedRange.Rows.Count
BTW, all the variables that you use to hold the number of rows should be defined as Long.
Should this be coded as a Sub or Function?  Could you give me an example of what the code would look like?
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. That worked great.
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013