Creating a Loop through a column....

Juddtron

New Member
Joined
Sep 20, 2023
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I am trying to figure out how to create a loop that will use the code I've written on all rows in a column, does anyone know what the looping code would look like?

Sub Average_Donation()
Dim r1 As Range, r2 As Range, r3 As Range
Dim iCell As Range

Set r1 = Range("E3")
Set r2 = Range("G3")
Set r3 = Range("P3")
If r1.Value <> 0 Then
r3.Value = r2.Value / r1.Value
End If
r3.NumberFormat = "00.0%"

For Each iCell In Range("r3").Cells

iCells = "Yes"
Next iCell
End Sub
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Welcome to the Board!

If you wanted to loop from cell R3 down to the last row in column R with data, you could do it like this:

VBA Code:
Dim lr as Long
lr = Cells(Rows.Count, "R").End(xlUp).Row
For Each iCell in Range("R3:R" & lr)
...

However, if you just wanted to set all the cells in that range equal to "Yes", there is no needs of loops. Just do:
VBA Code:
Dim lr as Long
lr = Cells(Rows.Count, "R").End(xlUp).Row
Range("R3:R" & lr).Value = "Yes"
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
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