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

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
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,223,730
Messages
6,174,162
Members
452,548
Latest member
Enice Anaelle

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