ForEach Maybe... Or just basic excel help

JohnCG

New Member
Joined
Jun 22, 2012
Messages
5
I am new to visual basic and I have been using the record macro feature to try and learn how it works but I have run into an issue. I have a list of values in column J that varies in length per sheet and I want each cell in column J that has a value to run through a macro. Placing the value from column J in T15, then running the macro and then having the result from U27 placed in the row corresponding to its column J location in column W. I have added the macro I wish to run once a value is placed in T15.
Code:
Sub AgrsvShift()
'
' AgrsvShift Macro
'

'
    Range("T16").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(R[-1]C>1,INDEX(C[-7],MATCH(R[-1]C,Extract,0)),""--"")"
    Range("T17").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-1]C/R[-1]C[1],""--"")"
    Range("T18").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-1]C*R[-7]C,""--"")"
    Range("U19").Select
    ActiveCell.FormulaR1C1 = "=IF(R[-6]C[-1]>1,R[-6]C[-1],""--"")"
    Range("T19").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(((RC[1]*R[-4]C[1])/R[-4]C),""--"")"
    Range("T20").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-1]C[1]-R[-1]C,""--"")"
    Range("T21").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-2]C+R[-2]C[1],""--"")"
    Range("U23").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-3]C[-1]*R[-5]C[-1],""--"")"
    Range("U24").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-3]C[-1]*R[-6]C,""--"")"
    Range("U25").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-4]C[-1]*(1-R[-14]C[-1]),""--"")"
    Range("U26").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-3]C-R[-2]C-R[-1]C,""--"")"
    Range("U27").Select
    ActiveCell.FormulaR1C1 = "=IFERROR(R[-1]C/R[-8]C,""--"")"
End Sub
Thanks in advance for any help:)
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
This seems to be a better code:

Code:
Sub AgrsvShift()
    Range("T16").Formula = "=IF(R[-1]C>1,INDEX(C[-7],MATCH(R[-1]C,Extract,0)),""--"")"
    Range("T17").Formula = "=IFERROR(R[-1]C/R[-1]C[1],""--"")"
    Range("T18").Formula = "=IFERROR(R[-1]C*R[-7]C,""--"")"
    Range("T19").Formula = "=IFERROR(((RC[1]*R[-4]C[1])/R[-4]C),""--"")"
    Range("T20").Formula = "=IFERROR(R[-1]C[1]-R[-1]C,""--"")"
    Range("T21").Formula = "=IFERROR(R[-2]C+R[-2]C[1],""--"")"
    Range("U19").Formula = "=IF(R[-6]C[-1]>1,R[-6]C[-1],""--"")"
    Range("U23").Formula = "=IFERROR(R[-3]C[-1]*R[-5]C[-1],""--"")"
    Range("U24").Formula = "=IFERROR(R[-3]C[-1]*R[-6]C,""--"")"
    Range("U25").Formula = "=IFERROR(R[-4]C[-1]*(1-R[-14]C[-1]),""--"")"
    Range("U26").Formula = "=IFERROR(R[-3]C-R[-2]C-R[-1]C,""--"")"
    Range("U27").Formula = "=IFERROR(R[-1]C/R[-8]C,""--"")"
End Sub
 
Upvote 0
For the loop:

Rich (BB code):
For Each rng In Range("J1:J100")
  If Len(rng.Value) then
    Range("T15").Value = rng.Value
    AgrsvShift
    Range("W" & Rng.Row).Value = Range("U27").Value 
  End If
Next

I do not see why you need to execute AgrsvShift a number of times, but this will give you initial code to experiment with.

Also, change the range in column J if needed.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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