View Single Post
  #8   Report Post  
Posted to microsoft.public.excel.programming
Clif McIrvin[_3_] Clif McIrvin[_3_] is offline
external usenet poster
 
Posts: 203
Default Looping through Non contiguous cells in a row

"GS" wrote in message
...
avi brought next idea :
Hello,

I want to loop accross each cell in a row oa a non contiguous
range,
something like

for I=1 to NumberOfRows
for J=1 to NumberOfCols
msgbox MyRange.Rows(I).cells(I,J).value
next
next

The problem is that the J seems to refer to the contiguous range
starting with first column of the range
Any help
Avi


Lets say that I have a non contiguous MyRange"B1:C10","E1:E10" of
numerical values

for I=1 to 10
for J=1 to 3
msgbox MyRange.cells(I,J).value
next
next

The problem is that for I=1 , J runs accross the contiguous rangerow
"B1:D1" instead of "B1:C1" and E1

Thanks
Avi


That's because E1 is the fourth cell in the range. Col4 is D.
Sounds to me like you need to look at using Areas, which can hold
non-contiguous ranges. Then you won't get the col count issue because
you would use a For Each loop to iterate each cell.

--
Garry

Free usenet access at http://www.eternal-september.org
ClassicVB Users Regroup! comp.lang.basic.visual.misc




Something like this:

Sub example()
Dim MyRange As Range
Dim a As Range
Dim intI As Integer
Dim intJ As Integer

Set MyRange = Union(Range("B1:C10"), Range("E1:E10"))

For intI = 1 To MyRange.Rows.Count
For Each a In MyRange.Areas
For intJ = 1 To a.Columns.Count
Debug.Print a.Cells(intI, intJ).Address
'MsgBox a.Cells(intI, intJ).Value
Next intJ
Next a
Next intI

Set MyRange = Nothing
End Sub

--
Clif McIrvin

(clare reads his mail with moe, nomail feeds the bit bucket :-)