<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Index Looping Through a Range</title>
	<atom:link href="http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/</link>
	<description>Daily posts of Excel tips…and other stuff</description>
	<lastBuildDate>Thu, 09 Feb 2012 23:42:03 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Alan Wright</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-28873</link>
		<dc:creator>Alan Wright</dc:creator>
		<pubDate>Fri, 23 Nov 2007 02:53:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-28873</guid>
		<description>&lt;p&gt;I was after similar/simple index access to cells within composite ranges.&lt;br&gt;
I found that COLLECTIONs of cells to replace the composite ranges allowed me to achieve this, viz;&lt;/p&gt;
&lt;p&gt;Private Function cCells(oRX As Range) As Collection&lt;br&gt;
Dim oCell As Range&lt;/p&gt;
&lt;p&gt;    Set cCells = New Collection&lt;br&gt;
    For Each oCell In oRX&lt;br&gt;
        cCells.Add oCell&lt;br&gt;
    Next oCell&lt;/p&gt;
&lt;p&gt;End Function&lt;/p&gt;
&lt;p&gt;ie my public functions called the cCells function...etc&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I was after similar/simple index access to cells within composite ranges.<br />
I found that COLLECTIONs of cells to replace the composite ranges allowed me to achieve this, viz;</p>
<p>Private Function cCells(oRX As Range) As Collection<br />
Dim oCell As Range</p>
<p>    Set cCells = New Collection<br />
    For Each oCell In oRX<br />
        cCells.Add oCell<br />
    Next oCell</p>
<p>End Function</p>
<p>ie my public functions called the cCells function&#8230;etc</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Peltier</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19365</link>
		<dc:creator>Jon Peltier</dc:creator>
		<pubDate>Mon, 03 Apr 2006 12:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19365</guid>
		<description>&lt;p&gt;For i = 1 to MyRange.Areas.Count&lt;br&gt;
For j = 1 to MyRange.Areas(i).Cells.Count&lt;br&gt;
&#039; blah blah blah&lt;br&gt;
Next j&lt;br&gt;
Next i&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>For i = 1 to MyRange.Areas.Count<br />
For j = 1 to MyRange.Areas(i).Cells.Count<br />
&#8216; blah blah blah<br />
Next j<br />
Next i</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tushar Mehta</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19361</link>
		<dc:creator>Tushar Mehta</dc:creator>
		<pubDate>Mon, 03 Apr 2006 00:06:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19361</guid>
		<description>&lt;p&gt;Jon: It&#039;s not very important.  In fact, IIRC, some MSKB article argues that For Each is more machine efficient.  But, for most *practical* uses, it is less human efficient.  In most cases where one wants to do something in sync with the control variable of a For Each loop, one is forced to implement a seperate index and manually sync it with the for each loop.  Something like:&lt;br&gt;
Dim I as Integer, aRng as Range&lt;br&gt;
I=1&lt;br&gt;
For each aRng...&lt;br&gt;
&#160;&#160;&#160;&#160;...&lt;br&gt;
&#160;&#160;&#160;&#160;I=I+1&lt;br&gt;
&#160;&#160;&#160;&#160;next aRng&lt;/p&gt;
&lt;p&gt;Dick:  Yeah, it&#039;s a pain.  In fact, I usually &quot;black-box&quot; such access with something similar -- though a lot simpler -- than Juan Pablo&#039;s suggestion.  I imagine he had a reason for his approach but it evades me.&lt;/p&gt;
&lt;p&gt;&lt;a href=&quot;http://tmc.newsgrouphosting.com/readmessage?id=%3C3586ab9b$1236047a$ab0d@mps%3E&amp;group=tmc.blogposts&quot; rel=&quot;nofollow&quot;&gt;http://tmc.newsgrouphosting.com/readmessage?id=%3C3586ab9b$1236047a$ab0d@mps%3E&amp;group=tmc.blogposts&lt;/a&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Jon: It&#8217;s not very important.  In fact, IIRC, some MSKB article argues that For Each is more machine efficient.  But, for most *practical* uses, it is less human efficient.  In most cases where one wants to do something in sync with the control variable of a For Each loop, one is forced to implement a seperate index and manually sync it with the for each loop.  Something like:<br />
Dim I as Integer, aRng as Range<br />
I=1<br />
For each aRng&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&#8230;<br />
&nbsp;&nbsp;&nbsp;&nbsp;I=I+1<br />
&nbsp;&nbsp;&nbsp;&nbsp;next aRng</p>
<p>Dick:  Yeah, it&#8217;s a pain.  In fact, I usually &#8220;black-box&#8221; such access with something similar &#8212; though a lot simpler &#8212; than Juan Pablo&#8217;s suggestion.  I imagine he had a reason for his approach but it evades me.</p>
<p><a href="http://tmc.newsgrouphosting.com/readmessage?id=%3C3586ab9b$1236047a$ab0d@mps%3E&amp;group=tmc.blogposts" rel="nofollow">http://tmc.newsgrouphosting.com/readmessage?id=%3C3586ab9b$1236047a$ab0d@mps%3E&#038;group=tmc.blogposts</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Peltier</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19356</link>
		<dc:creator>Jon Peltier</dc:creator>
		<pubDate>Sat, 01 Apr 2006 14:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19356</guid>
		<description>&lt;p&gt;Why is it so important not to use For Each?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Why is it so important not to use For Each?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: fzz</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19354</link>
		<dc:creator>fzz</dc:creator>
		<pubDate>Sat, 01 Apr 2006 02:20:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19354</guid>
		<description>&lt;p&gt;First off, in the Immediate window in the VBE,&lt;/p&gt;
&lt;p&gt;? Range(&quot;C5?).Item(3).Address(0, 0)&lt;br&gt;
C7&lt;br&gt;
? Range(&quot;C5?).Cells(3).Address(0, 0)&lt;br&gt;
C7&lt;/p&gt;
&lt;p&gt;The Item and Cells properties don&#039;t appear to be restricted to the cells in the Range.&lt;/p&gt;
&lt;p&gt;I don&#039;t see how you avoid iterating through the range either using For Each or by area then by cell within area, so best to do it only once rather than repeatedly.&lt;/p&gt;
&lt;p&gt;Sub test()&lt;br&gt;
    Dim r As Range, c As Collection&lt;br&gt;
    Set r = Range(&quot;A2,A4:A5,A7?)&lt;br&gt;
    Set c = mkcoll(r)&lt;br&gt;
    Debug.Print c(1).Address(0, 0)&lt;br&gt;
    Debug.Print c(2).Address(0, 0)&lt;br&gt;
    Debug.Print c(3).Address(0, 0)&lt;br&gt;
    Debug.Print c(4).Address(0, 0)&lt;br&gt;
    Debug.Print c(5).Address(0, 0)&lt;br&gt;
End Sub&lt;/p&gt;
&lt;p&gt;Function makecollection(x As Object) As Collection&lt;br&gt;
    Dim nc As New Collection, v As Variant&lt;br&gt;
    On Error GoTo Finish&lt;br&gt;
    For Each v In x&lt;br&gt;
        nc.Add Item:=v&lt;br&gt;
    Next v&lt;br&gt;
    Set makecollection = nc&lt;br&gt;
Finish:&lt;br&gt;
End Function&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>First off, in the Immediate window in the VBE,</p>
<p>? Range(&#8220;C5?).Item(3).Address(0, 0)<br />
C7<br />
? Range(&#8220;C5?).Cells(3).Address(0, 0)<br />
C7</p>
<p>The Item and Cells properties don&#8217;t appear to be restricted to the cells in the Range.</p>
<p>I don&#8217;t see how you avoid iterating through the range either using For Each or by area then by cell within area, so best to do it only once rather than repeatedly.</p>
<p>Sub test()<br />
    Dim r As Range, c As Collection<br />
    Set r = Range(&#8220;A2,A4:A5,A7?)<br />
    Set c = mkcoll(r)<br />
    Debug.Print c(1).Address(0, 0)<br />
    Debug.Print c(2).Address(0, 0)<br />
    Debug.Print c(3).Address(0, 0)<br />
    Debug.Print c(4).Address(0, 0)<br />
    Debug.Print c(5).Address(0, 0)<br />
End Sub</p>
<p>Function makecollection(x As Object) As Collection<br />
    Dim nc As New Collection, v As Variant<br />
    On Error GoTo Finish<br />
    For Each v In x<br />
        nc.Add Item:=v<br />
    Next v<br />
    Set makecollection = nc<br />
Finish:<br />
End Function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Randy Harmelink</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19349</link>
		<dc:creator>Randy Harmelink</dc:creator>
		<pubDate>Fri, 31 Mar 2006 19:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19349</guid>
		<description>&lt;p&gt;&gt; ?rAllFound.Address&lt;br&gt;
&gt; $A$2,$A$4:$A$5,$A$7&lt;br&gt;
&gt;&lt;br&gt;
&gt; There are four cells, as you can see.&lt;/p&gt;
&lt;p&gt;I think this concept is where you&#039;re running into the problem.  What you are listing there is NOT four cells, it&#039;s three ranges (aka &quot;areas&quot;) that in total contain four cells.  The UNION() function does just that -- it creates a UNION of ranges -- not a new range of individual cells.  Hence the need to naviagate through the AREAS before the CELLS, as in:&lt;/p&gt;
&lt;p&gt;    Set rAllFound = Application.Union(Range(&quot;$A$2?), Range(&quot;$A$4:$A$5?), Range(&quot;$A$7?))&lt;br&gt;
    For i1 = 1 To rAllFound.Areas.Count&lt;br&gt;
        For i2 = 1 To rAllFound.Areas(i1).Cells.Count&lt;br&gt;
            Debug.Print rAllFound.Areas(i1).Cells(i2).Address&lt;br&gt;
            Next i2: Next i1&lt;/p&gt;
&lt;p&gt;Here&#039;s an IMPORTANT note -- the order you specify ranges in the UNION() statement may NOT be the same order they end up in as areas and cells.  For example, suppose I have these three UNION() statements:&lt;/p&gt;
&lt;p&gt;    Set rAllFound1 = Application.Union(Range(&quot;$A$2?), Range(&quot;$A$4:$A$5?), Range(&quot;$A$7?))&lt;br&gt;
    Set rAllFound2 = Application.Union(Range(&quot;$A$2?), Range(&quot;$A$4?), Range(&quot;$A$7?), Range(&quot;$A$5?))&lt;br&gt;
    Set rAllFound3 = Application.Union(Range(&quot;$A$2?), Range(&quot;$A$7?), Range(&quot;$A$4:$A$5?))&lt;/p&gt;
&lt;p&gt;Here are the addresses we end up with for each:&lt;/p&gt;
&lt;p&gt;    rAllFound1.Address = $A$2,$A$4:$A$5,$A$7&lt;br&gt;
    rAllFound2.Address = $A$2,$A$7,$A$4:$A$5&lt;br&gt;
    rAllFound3.Address = $A$2,$A$7,$A$4:$A$5&lt;/p&gt;
&lt;p&gt;Note that on the rAllFound2 UNION(), the function automatically combined the $A$4 and $A$5 single-cell areas into a single area because they were adjacent.  Which reiterates the point that we are not creating a new range -- just a UNION() of ranges.  If you add another range with $B$2 in it, the $A$2 range would move to join it.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>&gt; ?rAllFound.Address<br />
&gt; $A$2,$A$4:$A$5,$A$7<br />
&gt;<br />
&gt; There are four cells, as you can see.</p>
<p>I think this concept is where you&#8217;re running into the problem.  What you are listing there is NOT four cells, it&#8217;s three ranges (aka &#8220;areas&#8221;) that in total contain four cells.  The UNION() function does just that &#8212; it creates a UNION of ranges &#8212; not a new range of individual cells.  Hence the need to naviagate through the AREAS before the CELLS, as in:</p>
<p>    Set rAllFound = Application.Union(Range(&#8220;$A$2?), Range(&#8220;$A$4:$A$5?), Range(&#8220;$A$7?))<br />
    For i1 = 1 To rAllFound.Areas.Count<br />
        For i2 = 1 To rAllFound.Areas(i1).Cells.Count<br />
            Debug.Print rAllFound.Areas(i1).Cells(i2).Address<br />
            Next i2: Next i1</p>
<p>Here&#8217;s an IMPORTANT note &#8212; the order you specify ranges in the UNION() statement may NOT be the same order they end up in as areas and cells.  For example, suppose I have these three UNION() statements:</p>
<p>    Set rAllFound1 = Application.Union(Range(&#8220;$A$2?), Range(&#8220;$A$4:$A$5?), Range(&#8220;$A$7?))<br />
    Set rAllFound2 = Application.Union(Range(&#8220;$A$2?), Range(&#8220;$A$4?), Range(&#8220;$A$7?), Range(&#8220;$A$5?))<br />
    Set rAllFound3 = Application.Union(Range(&#8220;$A$2?), Range(&#8220;$A$7?), Range(&#8220;$A$4:$A$5?))</p>
<p>Here are the addresses we end up with for each:</p>
<p>    rAllFound1.Address = $A$2,$A$4:$A$5,$A$7<br />
    rAllFound2.Address = $A$2,$A$7,$A$4:$A$5<br />
    rAllFound3.Address = $A$2,$A$7,$A$4:$A$5</p>
<p>Note that on the rAllFound2 UNION(), the function automatically combined the $A$4 and $A$5 single-cell areas into a single area because they were adjacent.  Which reiterates the point that we are not creating a new range &#8212; just a UNION() of ranges.  If you add another range with $B$2 in it, the $A$2 range would move to join it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Juan Pablo Gonzalez</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19347</link>
		<dc:creator>Juan Pablo Gonzalez</dc:creator>
		<pubDate>Fri, 31 Mar 2006 18:58:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19347</guid>
		<description>&lt;p&gt;Yes, there&#039;s no easy way... you have to include the Areas in there.  I came up with this function does seems to work:&lt;/p&gt;
&lt;p&gt;Sub Test()&lt;br&gt;
   Dim Rng As Range&lt;br&gt;
   Dim i As Long&lt;/p&gt;
&lt;p&gt;   Set Rng = Range(&quot;$A$2,$A$4:$A$5,$A$7?)&lt;/p&gt;
&lt;p&gt;   For i = 1 To Rng.Count&lt;br&gt;
      Debug.Print i, IndexArea(Rng, i).Address&lt;br&gt;
   Next i&lt;br&gt;
End Sub&lt;/p&gt;
&lt;p&gt;Function IndexArea(ByVal Rng As Range, ByVal Index As Long) As Range&lt;br&gt;
   Dim areaCounter As Long&lt;br&gt;
   Dim i As Long&lt;/p&gt;
&lt;p&gt;   i = 0&lt;br&gt;
   Do&lt;br&gt;
      i = i + 1&lt;br&gt;
      areaCounter = areaCounter + Rng.Areas(i).Count&lt;br&gt;
   Loop Until areaCounter &gt; = Index&lt;/p&gt;
&lt;p&gt;   If areaCounter = Index Then&lt;br&gt;
      Set IndexArea = Rng.Areas(i).Item(Rng.Areas(i).Count)&lt;br&gt;
   Else&lt;br&gt;
      Set IndexArea = Rng.Areas(i).Item(areaCounter - Index)&lt;br&gt;
   End If&lt;br&gt;
End Function&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Yes, there&#8217;s no easy way&#8230; you have to include the Areas in there.  I came up with this function does seems to work:</p>
<p>Sub Test()<br />
   Dim Rng As Range<br />
   Dim i As Long</p>
<p>   Set Rng = Range(&#8220;$A$2,$A$4:$A$5,$A$7?)</p>
<p>   For i = 1 To Rng.Count<br />
      Debug.Print i, IndexArea(Rng, i).Address<br />
   Next i<br />
End Sub</p>
<p>Function IndexArea(ByVal Rng As Range, ByVal Index As Long) As Range<br />
   Dim areaCounter As Long<br />
   Dim i As Long</p>
<p>   i = 0<br />
   Do<br />
      i = i + 1<br />
      areaCounter = areaCounter + Rng.Areas(i).Count<br />
   Loop Until areaCounter &gt; = Index</p>
<p>   If areaCounter = Index Then<br />
      Set IndexArea = Rng.Areas(i).Item(Rng.Areas(i).Count)<br />
   Else<br />
      Set IndexArea = Rng.Areas(i).Item(areaCounter &#8211; Index)<br />
   End If<br />
End Function</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex J</title>
		<link>http://www.dailydoseofexcel.com/archives/2006/03/31/index-looping-through-a-range/#comment-19346</link>
		<dc:creator>Alex J</dc:creator>
		<pubDate>Fri, 31 Mar 2006 18:48:00 +0000</pubDate>
		<guid isPermaLink="false">http://www.dailydoseofexcel.com/?p=1384#comment-19346</guid>
		<description>&lt;p&gt;My guess would be something like:&lt;/p&gt;
&lt;p&gt;Dim rArea as Range&lt;br&gt;
Dim rCel as Range&lt;/p&gt;
&lt;p&gt;For Each rArea in rAllfound.Areas&lt;br&gt;
    For Each rCel in rArea.Cells&lt;br&gt;
        &#039;Do Something&lt;br&gt;
    next rCel&lt;br&gt;
Next rArea&lt;/p&gt;
&lt;p&gt;(or use &#039;For a = 1 to areas.count&#039; and &#039;For c = 1 to cells.count&#039; if you REALLY like indicies)&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>My guess would be something like:</p>
<p>Dim rArea as Range<br />
Dim rCel as Range</p>
<p>For Each rArea in rAllfound.Areas<br />
    For Each rCel in rArea.Cells<br />
        &#8216;Do Something<br />
    next rCel<br />
Next rArea</p>
<p>(or use &#8216;For a = 1 to areas.count&#8217; and &#8216;For c = 1 to cells.count&#8217; if you REALLY like indicies)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

