how to recycle through cells in a selected range

S

stub

using windows XP; created macro to look at each cell in a one column range.
named range. dim. variables. selected worksheet.selected named range.
created for next cell in seledction. see following
Sheets("Sheet2").Select
Range("I16.I45").Select
For Each cell In Selection
If ActiveCell.Value = False Then
ActiveCell.Offset(0, -2).Value = ActiveCell.Offset(0, -2).Value +
Range("step").Value
End If
Next cell
the offset value is increased until the active cell is not false but the
program does not loop to the next cell in the range. how do i correct?
 
C

Chip Pearson

A For Each loop does NOT change what cell is active. As it loops, it sets
the control variable ("cell" in your example code) to each cell in the given
range, but unless you explicitly activate the cell, the ActiveCell remains
what it was prior to the loop. Instead of

ActiveCell.Value

use

Cell.Value

or activate cell:

Cell.Activate


--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 

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