Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

excel vba

Posted on 2013-10-24
8
Medium Priority
?
317 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

Outlook for dependable use in a very small business   This article is about using the Outlook application (part of Microsoft Office) in a very small business, or for homeowners where dependability and reliability are critical requirements. This …
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

877 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