Counting Rows in Excel Macro

R

Randy Wiseman

I need to loop through all the rows in an MS Excel
Worksheet and would like to use a while construct like
below. The problem is that Worksheets().Range
().Rows.Count returns the maximum number of rows possible
instead of the actual number of rows. What am I doing
wrong?

MyRowCount = Worksheets("MySheet").Range("A").Rows.Count

while MyRowPtr < MyRowCount
examine the row
conditionally do some stuff
wend
 
C

Chip Pearson

Randy,

You can use the UsedRange property to get range that actually is
in use.

MyRowCount = Worksheets(1).UsedRange.Rows.Count


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top