How might I reference the cell currently being looked at within a `For Each` loop?

JRCHAPMAN

New Member
Joined
Aug 6, 2018
Messages
4
I am currently trying to learn the basics of VBA and have been using the following code to work on a column of cells

Code:
For Each foo In Range("A1", "A20")
    * Code *
Next

How might I return the cell being referenced at each iteration of this loop?

For example, suppose cells A1 to A20 contained integers and for each of these values I wished to write the square root in cells B1 to B20. How might this be done?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
I thought that this might be possible using the code
Code:
For Each foo In Range("A1", Range("A1").End(xlToRight)).Cells
    MsgBox (foo.RowIndex)
Next
but this did not work.
 
Upvote 0
I am currently trying to learn the basics of VBA and have been using the following code to work on a column of cells

Code:
For Each foo In Range("A1", "A20")
    * Code *
Next

How might I return the cell being referenced at each iteration of this loop?

For example, suppose cells A1 to A20 contained integers and for each of these values I wished to write the square root in cells B1 to B20. How might this be done?
The variable foo automatically references the cells in range A1:A20 one at a time, so you would write it like this...
Code:
For Each foo In Range("A1", "A20")
  foo.Offset(0, 1) = Sqr(foo)
Next
 
Last edited:
Upvote 0
In your code "foo" is just a range value.
So to gets its value inside the loop, you can use:
foo.Value

To get its address, you can use:
foo.Address
 
Upvote 0
.RowIndex is not a property of a Range object.

The loops you've set up are looping with the variable foo. Try something like

Code:
Dim foo as Range

For Each foo in Range("A1:D3")
    MsgBox foo.Address & " on row " & foo.Row
Next foo
 
Upvote 0
Hi & welcome to MrExcel
How about
Code:
Sub Squareroot()
   Dim Cl As Range
   
   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      Cl.Offset(, 1).Value = Sqr(Cl.Value)
   Next Cl
End Sub
 
Upvote 0
Thanks for all the help guys! Great to now be able to see several different ways of tackling this problem.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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