View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Non-Contiguos Range Iteration

set rng = Range("J313:L315,T313:U315")

for i = 313 to 315

for each cell in Intersect(rows(i),rng).Cells
cell.Value = i - 313
Next

Next

--
Regards,
Tom Ogivy

"Matthew Wieder" wrote in message
...
I have a complex calculation that I need to write in VBA which can be
represented with the following example. I need to iterate through all
the cells in a noncontiguous range and starting from 1, put the number
of the rows iterated through into the cells in that row so I end up with
something like this:
J K L T U


313 1 1 1 1 1
314 2 2 2 2 2
315 3 3 3 3 3

The problem is that the method for iterating through a noncontiguous
range treats the cells as unrelated and hence I would have to iterate
through the J313:L315 range and only then go on to the T313:U315 range,
in which case I lose the count of the rows. (in the actual case, the
values to be placed in the rows is much more difficult to re-compute
then the row number). What I need to do, is iterate through row 313
(J-L, T-U), then row 314(same) then row 315(same).

Is there an easier way to do this?
thanks!