Solved

excel vba

Posted on 2013-10-24
8
287 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
  • 5
  • 3
8 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
Comment Utility
What is the symbol "¿" that you mentioned?
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
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
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
BTW, all the variables that you use to hold the number of rows should be defined as Long.
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 

Author Comment

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

Accepted Solution

by:
Martin Liss earned 500 total points
Comment Utility
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
Comment Utility
Thanks. That worked great.
0
 
LVL 45

Expert Comment

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

Marty - MVP 2009 to 2013
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now