Set MyRange for non-adjacent cells and iterate through?

E

Ed

What I have in mind to try and do would be easy if I could iterate through
all the cells of a specified block of cells, picking out certain cells that
meet a specific criterion, and adding them to MyRange object. These cells
would probably be scattered over the original block of cells and many would
not be adjacent to any other cell in MyRange. Then I would iterate through
all the cells in MyRange to do things, and drop cells out of MyRange until I
have exactly what I want. Is it possible to set up a range of non-adjacent
cells so they can be iterated through? If so, what's the easiest methods to
use to add or delete a cell from such a range?

Thank you.
Ed
 
D

Dave Peterson

One way...

dim myRng as range
dim myCell as range
dim mySubsetRng as range

set myrng = activesheet.range("a1:c9")
for each mycell in myrng.cells
if mycell.value = lcase(mycell.value) then
if mysubsetrng is nothing then
set mysubsetrng = mycell
else
set mysubsetrng = union(mysubsetrng,mycell)
end if
end if
next mycell

for each mycell in mysubsetrng.cells
msgbox mycell.value & vblf & mycell.address
next mycell
 
E

Ed

Thank you, Dave! It works great!

Ed

Dave Peterson said:
One way...

dim myRng as range
dim myCell as range
dim mySubsetRng as range

set myrng = activesheet.range("a1:c9")
for each mycell in myrng.cells
if mycell.value = lcase(mycell.value) then
if mysubsetrng is nothing then
set mysubsetrng = mycell
else
set mysubsetrng = union(mysubsetrng,mycell)
end if
end if
next mycell

for each mycell in mysubsetrng.cells
msgbox mycell.value & vblf & mycell.address
next mycell
 

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