Looping through range newbie question

F

Frank Rizzo

Sorry, for what maybe a really simple question - i am a newbie.

I've found that I can loop through a range via the following statement:

For Each c In MyRange.Cells
Debug.Print c.Value
Next

However, I need to know when the next row starts, so I am looking to
loop using the following construct:

For Each oRow In MyRange.Rows
For Each oCol In MyRange.Columns(oRow.index)
Debug.Print oCol.Cell.Value
Next
Next

But I can't figure out how to make it work. Is this possible? Or maybe
I am taking the wrong approach?

Thanks
 
R

Robin Hammond

Frank,

Try something like this

Sub PrintRangeValues(rngOutput As Range)
Dim lRow As Long
Dim nCol As Integer
With rngOutput
For lRow = 1 To .Rows.Count
For nCol = 1 To .Columns.Count
Debug.Print .Cells(lRow, nCol).Value
Next nCol
Next lRow
End With
End Sub

Robin Hammond
www.enhanceddatasystems.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top