Solved

excel vba

Posted on 2013-10-24
8
293 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 46

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 46

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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 46

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 46

Accepted Solution

by:
Martin Liss earned 500 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 46

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
Outlook Free & Paid Tools
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

770 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