Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
avi avi is offline
external usenet poster
 
Posts: 195
Default Looping through Non contiguous cells in a row

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
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 203
Default Looping through Non contiguous cells in a row

"avi" wrote in message
...
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



try changing
msgbox MyRange.Rows(I).cells(I,J).value


to
msgbox MyRange.cells(I,J).value

--
Clif McIrvin

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


  #3   Report Post  
Posted to microsoft.public.excel.programming
avi avi is offline
external usenet poster
 
Posts: 195
Default Looping through Non contiguous cells in a row

Thanks but I get the same wrong columns

Avi
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 203
Default Looping through Non contiguous cells in a row

"avi" wrote in message
...
Thanks but I get the same wrong columns

Avi



In that case, we need more information. From your OP I really couldn't
tell what you are really trying to do. How are you creating your range,
for one. More of a step-by-step description of what you are doing would
help.

Also, it is helpful if you keep enough of the OP in the reply to keep
the context ... those of us using NNTP newsreaders instead of a web
interface generally do not have previous postings in the thread
available ... and that includes many, if not most, of the regulars here.

--
Clif McIrvin

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


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Looping through Non contiguous cells in a row

avi laid this down on his screen :
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


I'm thinking a problem exists with the value held in 'NumberOfCols'.
Are you using 'UsedRange.Columns.Count'? Or some other method to
determine the last column used. I would use a method similar to finding
the last row containing data. Something using 'End' and xlToLeft to
determine NumberOfCols.

--
Garry

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




  #6   Report Post  
Posted to microsoft.public.excel.programming
avi avi is offline
external usenet poster
 
Posts: 195
Default Looping through Non contiguous cells in a row


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
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Looping through Non contiguous cells in a row

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


  #8   Report Post  
Posted to microsoft.public.excel.programming
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 :-)


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 226
Default Looping through Non contiguous cells in a row

Clif McIrvin presented the following explanation :
"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


Very good! That does exactly what the OP asks for. The use of the
Union() function (which I failed to mention) is what makes it work.

--
Garry

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


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Non-contiguous cells Kokomojo Excel Discussion (Misc queries) 2 November 6th 07 04:09 PM
Copy and Paste LAST ROW of data: non-contiguous Row, contiguous Column Sam via OfficeKB.com Excel Programming 8 November 5th 07 07:18 PM
Looping over non-contiguous column selection Jim Hagan Excel Programming 2 June 14th 07 07:16 PM
counting cells that are 0 in a range of non-contiguous cells Mark Excel Worksheet Functions 9 March 14th 07 02:45 PM
How do I count non-contiguous cells? broush Excel Worksheet Functions 5 May 30th 06 11:16 PM


All times are GMT +1. The time now is 07:08 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"