ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Looping through range newbie question (https://www.excelbanter.com/excel-programming/315721-looping-through-range-newbie-question.html)

Frank Rizzo

Looping through range newbie question
 
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

Robin Hammond[_2_]

Looping through range newbie question
 
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

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





All times are GMT +1. The time now is 10:01 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com