ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Loop through range issue (https://www.excelbanter.com/excel-programming/371426-loop-through-range-issue.html)

Kieran[_55_]

Loop through range issue
 
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran


Don Guillett

Loop through range issue
 
how about this

For i = 1 To 20
MsgBox Cells(i, 1).Address
Next i

--
Don Guillett
SalesAid Software

"Kieran" wrote in message
oups.com...
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran




NickHK[_3_]

Loop through range issue
 
Kieran,
I would assume that because you specify .Columns(1), the actual range is
consider to be the whole block (A1:A20), of which the is only 1.

However if you loop through
For Each Cell In rRange.Cells
you are returning to a range of constituent cells and get your desired
results.

Seems like a lame explanation even to me, so hopefully somebody has a better
one.

NickHK

"Kieran"
egroups.com...
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran




Tom Ogilvy

Loop through range issue
 
From the immediate window:
? Range("A1:B20").Columns(1).Count
1
? Range("A1:B20").Columns(1).Cells.count
20

You have 1 "column" range in the first case.

so you would want:

Set rRange = Range("A1:B20").Columns(1).Cells

--
Regards,
Tom Ogilvy




"Kieran" wrote:

Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran



Jean-Yves[_2_]

Loop through range issue
 
Hi Kieran
Interesting
For resizing a range use "resize" instead of using the columns property.
Set rRange = Range("A1:B20").Resize(, 1)

For the why part, could a better person answer this ?
Regards
Jean-Yves


"Kieran" wrote in message
oups.com...
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran




Kieran[_55_]

Loop through range issue
 
To all:

The mist clears . . . . .

Set rRange = Range("A1:B20").Columns(1)

rRange is a monlithic block containing one cell - Negative outcome -
not good!

Set rRange = Range("A1:B20").Columns(1).Cells

rRange contains all cells as desired.

Yet another case of Pilot Error!

Many Thanks


Jean-Yves wrote:
Hi Kieran
Interesting
For resizing a range use "resize" instead of using the columns property.
Set rRange = Range("A1:B20").Resize(, 1)

For the why part, could a better person answer this ?
Regards
Jean-Yves


"Kieran" wrote in message
oups.com...
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran



Alan Beban

Loop through range issue
 
Kieran wrote:
To all:

The mist clears . . . . .


Not quite. After Set rRange = Range("A1:B20").Columns(1)

rRange.Address returns $A$1:$A$20, as does rRange(1).Address.
rRange(2).Address returns $B$1:$B$20, rRange(3).Address $C$1:$C$20, etc.
This might be the monolithicality you were referring to. But rRange
contains not just one cell, but all the cells in rRange, as you can see
from, e.g., rRange.Cells(3).Address, which returns $A$3; you just can't
access the individual cells with rRange(1), rRange(2), etc., because
those index numbers refer to columns, not cells.

Alan Beban


Set rRange = Range("A1:B20").Columns(1)

rRange is a monlithic block containing one cell - Negative outcome -
not good!

Set rRange = Range("A1:B20").Columns(1).Cells

rRange contains all cells as desired.

Yet another case of Pilot Error!

Many Thanks


Jean-Yves wrote:
Hi Kieran
Interesting
For resizing a range use "resize" instead of using the columns property.
Set rRange = Range("A1:B20").Resize(, 1)

For the why part, could a better person answer this ?
Regards
Jean-Yves


"Kieran" wrote in message
oups.com...
Greetings all -

My problem is one of understanding or lack of it! :

I wish to loop through the first column of a range. I set a range
object to equal the first column of my range. I then attempt to loop
through each cell in my now - single column range. Yet for reasons I
don't understand my Cell range is now the same as rRange and the
process fails

Sub xx()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In rRange
Debug.Print Cell.Address
Next
End Sub

However if I do the following it works perfectly

Sub x()
Dim Cell As Range
Dim rRange As Range

Set rRange = Range("A1:B20").Columns(1)
For Each Cell In Range(rRange.Address)
Debug.Print Cell.Address
Next
End Sub

Any thoughts would be appreciated - note that I am keen to understand
the issue not solve it another way

Cheers

Kieran




All times are GMT +1. The time now is 06:24 AM.

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