Counting Rows in Excel Macro

  • Thread starter Thread starter Randy Wiseman
  • Start date Start date
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
 
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
 
Back
Top