Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

excel vba

Posted on 2013-10-24
8
Medium Priority
?
312 Views
Last Modified: 2013-10-24
Does anyone have a good example of how to determine the number of used rows and columns in a spreadsheet?
0
Comment
Question by:tesla764
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39597819
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
0
 

Author Comment

by:tesla764
ID: 39597838
What is the symbol "¿" that you mentioned?
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39597888
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
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 49

Expert Comment

by:Martin Liss
ID: 39597892
BTW, all the variables that you use to hold the number of rows should be defined as Long.
0
 

Author Comment

by:tesla764
ID: 39597945
Should this be coded as a Sub or Function?  Could you give me an example of what the code would look like?
0
 
LVL 49

Accepted Solution

by:
Martin Liss earned 2000 total points
ID: 39597980
Dim lngLastRow as Long
Dim lngRow As Long

lngLastRow = Range("A65536").End(xlUp).Row

For lngRow = 1 to  lngLastRow 
    If Cells(lngRow, 1) = "checking column A" Then
        ' Do something
    End If
Next

Open in new window

0
 

Author Closing Comment

by:tesla764
ID: 39597997
Thanks. That worked great.
0
 
LVL 49

Expert Comment

by:Martin Liss
ID: 39598027
You're welcome and I'm glad I was able to help.

Marty - MVP 2009 to 2013
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a serious pitfall that can happen when deleting shapes using VBA.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question