Reading Active Cell Value in a For Next Loop

Ed S.

Board Regular
Joined
Mar 26, 2002
Messages
90
The following code works as needed with one problem - RNAME is only loading Values in the first ActiveCell range. The value of RNAME is not changing to reflect the "next cells" value in the For Next loop.

' Range("A1").Select
y = 1
x = 1
' Position to last Column of WorkSheet.
Selection.End(xlToRight).Select
z = ActiveCell.Column

'Select Range.
Range(Cells(x, y), Cells(x, z)).Select

For Each Cell In Selection
RName = ActiveCell.Offset(1, 0).Value
.
. 'Other work here (Need ActiveCell value)
.

Next Cell
(RName is not Changing after next cell???
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Add activecell.offset(1,0).select after
RName = ActiveCell.Offset(1, 0).Value
You're not scrolling through the activecell values, only the cells in the selection.
 
Upvote 0
Just adding that line might not work, but it should make you see that the for each ... next statement doesn't actually activate cells. The active cell is the original cell that is active and it's offsetting 1 row everytime and giving you the same name. You need to add a line that will offset the activecell by one every loop. That's what I meant by my first response.

Hope this helps,

Dave
 
Upvote 0
Dave - thanks.

Your second explanation was what I needed. I thought next cell activated the next cell...

Here is the code that now is working...

For Each Cell In Selection
RName = ActiveCell.Offset(1, 0).Value

ActiveCell.Offset(0, 1).Select_ ActiveWorkbook.Names.AddName:=RName,_ RefersToR1C1:="=SalesDetail!C2"

Next Cell

I am building a large Reference Name library based on an import of file. What a time saver...

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,920
Members
448,533
Latest member
thietbibeboiwasaco

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top