View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Chip Pearson Chip Pearson is offline
external usenet poster
 
Posts: 7,247
Default Use "For... Each" to Iterate Through a Cells Collection?

Try

Dim Rng As Range
For Each Rng In rngMyRange.Cells
Debug.Print Rng.Text
Next Rng


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com


"BobbyMurcerFan"
<BobbyMurcerFan.24vqrb_1142709601.6373@excelforu m-nospam.com
wrote in message
news:BobbyMurcerFan.24vqrb_1142709601.6373@excelfo rum-nospam.com...

In Excel 2000 VBA, Is it possible to use "For... Each" to
iterate
through a Cells collection?

Something along the lines of:

Code:
--------------------

' ILLUSTRATIVE PSUEDO CODE THAT DOESN'T WORK

Dim celX as Cell ' I realze that Excel's VBA doesn't
actually have a Cell object


' Iterate Through rngMyRange's Cells Collection
For Each celX in rngMyRange.Cells

MsgBox(celX.Value)

Next celX
--------------------

While I realize the above code won't actually work, IS there a
way to
use For Each with the Cells collection?

Presently, I'm using the Count property to get the number of
cells and
using that value in a For Loop iteration test.

Here's and example:

Code:
--------------------
' WON'T WORK FOR CELL OBJECT
Sub ForLoopCell()

Dim iCntr As Integer, rngTest As Range

Set rngTest = ActiveSheet.Range("B1:B10")


' Iterate Through Cells Collection By Using A Counter
For iCntr = 1 To rngTest.Cells.Count

MsgBox (rngTest.Cells(iCntr).Value)

Next iCntr

End Sub
--------------------


Using a For... Each would be easier, but is there a way to do
it?

THANKS VERY MUCH!! :)


--
BobbyMurcerFan
------------------------------------------------------------------------
BobbyMurcerFan's Profile:
http://www.excelforum.com/member.php...o&userid=11107
View this thread:
http://www.excelforum.com/showthread...hreadid=523941