<?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: Weird looping through cells in a range object</title>
	<atom:link href="http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/</link>
	<description>Supercharge Excel</description>
	<lastBuildDate>Thu, 26 Jan 2012 07:52:15 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Bastien</title>
		<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/comment-page-1/#comment-35175</link>
		<dc:creator>Bastien</dc:creator>
		<pubDate>Fri, 24 Oct 2008 08:10:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/#comment-35175</guid>
		<description>Hello Dave, Dick, Jon and Peder,

Wow,

I didn&#039;t know about the Range.Cells. I just figured that the &quot;for each&quot; would loop through the tiniest part of the collection: a cell, which it always did until I recently added the .Column(1) part.

However it seems logical to add the .Cells now that I read it.

Thank you!</description>
		<content:encoded><![CDATA[<p>Hello Dave, Dick, Jon and Peder,</p>
<p>Wow,</p>
<p>I didn't know about the Range.Cells. I just figured that the "for each" would loop through the tiniest part of the collection: a cell, which it always did until I recently added the .Column(1) part.</p>
<p>However it seems logical to add the .Cells now that I read it.</p>
<p>Thank you!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Peder Schmedling</title>
		<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/comment-page-1/#comment-35169</link>
		<dc:creator>Peder Schmedling</dc:creator>
		<pubDate>Fri, 24 Oct 2008 05:25:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/#comment-35169</guid>
		<description>I believe what you want to do is specify explicit what collection you want to iterate through:
[code]
Sub WeirdCelLoopingFixed()
      Dim rngCel                                       As Range
      Dim rngSelection                                 As Range

      Range(&quot;A1:G10&quot;).Select

      &#039; Works correctly:
      For Each rngCel In Selection.Columns(1).Cells
            Debug.Print rngCel.Address &amp; &quot; as part of &quot; &amp; Selection.Columns(1).Address
      Next

      &#039; Works correctly:
      Set rngSelection = Selection.Columns(1)
      For Each rngCel In rngSelection.Cells
            Debug.Print rngCel.Address &amp; &quot; as part of &quot; &amp; rngSelection.Address
      Next
End Sub
[/code]

I have no explanation for this but at least it works :-)
(remember to loop through areas also when your working with user selection)</description>
		<content:encoded><![CDATA[<p>I believe what you want to do is specify explicit what collection you want to iterate through:<br />
[code]<br />
Sub WeirdCelLoopingFixed()<br />
      Dim rngCel                                       As Range<br />
      Dim rngSelection                                 As Range</p>
<p>      Range("A1:G10").Select</p>
<p>      ' Works correctly:<br />
      For Each rngCel In Selection.Columns(1).Cells<br />
            Debug.Print rngCel.Address &amp; " as part of " &amp; Selection.Columns(1).Address<br />
      Next</p>
<p>      ' Works correctly:<br />
      Set rngSelection = Selection.Columns(1)<br />
      For Each rngCel In rngSelection.Cells<br />
            Debug.Print rngCel.Address &amp; " as part of " &amp; rngSelection.Address<br />
      Next<br />
End Sub<br />
[/code]</p>
<p>I have no explanation for this but at least it works :-)<br />
(remember to loop through areas also when your working with user selection)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jon Peltier</title>
		<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/comment-page-1/#comment-35168</link>
		<dc:creator>Jon Peltier</dc:creator>
		<pubDate>Fri, 24 Oct 2008 05:21:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/#comment-35168</guid>
		<description>Don&#039;t assume you know what will be the default member of a class. Use these aand the first two willl work properly:

For Each rngCel In Selection.Columns(1).Cells
For Each rngCel In rngSelection.Cells

For further assurance, the third should be:

For Each rngCel In rngSelection.Cells</description>
		<content:encoded><![CDATA[<p>Don't assume you know what will be the default member of a class. Use these aand the first two willl work properly:</p>
<p>For Each rngCel In Selection.Columns(1).Cells<br />
For Each rngCel In rngSelection.Cells</p>
<p>For further assurance, the third should be:</p>
<p>For Each rngCel In rngSelection.Cells</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dick Kusleika</title>
		<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/comment-page-1/#comment-35158</link>
		<dc:creator>Dick Kusleika</dc:creator>
		<pubDate>Thu, 23 Oct 2008 15:55:09 +0000</pubDate>
		<guid isPermaLink="false">http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/#comment-35158</guid>
		<description>That&#039;s the goofy nature of the range object, but it&#039;s consistent and actually useful in some situations.  When you reference Columns(1) you&#039;re referencing a range that is a column.  Just because you name your variable rngCel doesn&#039;t mean that it&#039;s a single cell range.  Excel fits the variable into the &quot;In&quot; reference&#039;s default return range.

Rows() and Columns() both return the entire row or column (within the range specified).  Range() and Areas() return the individual cells by default.

I&#039;ve developed the habit of always explicitly calling out the level of detail I want.  So I would code

For Each rng in Range(&quot;A1:G10&quot;).Cells

to mean that I want to loop through all cells individually (even though Cells is the default for Range), and

For Each rng in Range(&quot;A1:G10&quot;).Rows

to mean that I want to loop through the rows.

If you change your first loop to &quot;Selection.Columns(1).Cells&quot;, you would force the desired behavior rather than rely on the default for the Columns() range reference.</description>
		<content:encoded><![CDATA[<p>That's the goofy nature of the range object, but it's consistent and actually useful in some situations.  When you reference Columns(1) you're referencing a range that is a column.  Just because you name your variable rngCel doesn't mean that it's a single cell range.  Excel fits the variable into the "In" reference's default return range.</p>
<p>Rows() and Columns() both return the entire row or column (within the range specified).  Range() and Areas() return the individual cells by default.</p>
<p>I've developed the habit of always explicitly calling out the level of detail I want.  So I would code</p>
<p>For Each rng in Range("A1:G10").Cells</p>
<p>to mean that I want to loop through all cells individually (even though Cells is the default for Range), and</p>
<p>For Each rng in Range("A1:G10").Rows</p>
<p>to mean that I want to loop through the rows.</p>
<p>If you change your first loop to "Selection.Columns(1).Cells", you would force the desired behavior rather than rely on the default for the Columns() range reference.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dave</title>
		<link>http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/comment-page-1/#comment-35157</link>
		<dc:creator>Dave</dc:creator>
		<pubDate>Thu, 23 Oct 2008 12:23:26 +0000</pubDate>
		<guid isPermaLink="false">http://www.asap-utilities.com/blog/index.php/2008/10/23/weird-looping-through-cells/#comment-35157</guid>
		<description>Try:
For Each rngCel In Selection.Columns(1).cells
and
For Each rngCel In rngSelection.cells</description>
		<content:encoded><![CDATA[<p>Try:<br />
For Each rngCel In Selection.Columns(1).cells<br />
and<br />
For Each rngCel In rngSelection.cells</p>
]]></content:encoded>
	</item>
</channel>
</rss>

