For-next loop with 2 variables

Rasberry

Board Regular
Joined
Aug 11, 2002
Messages
195
Office Version
  1. 365
How do I get my loop to change the p and i at the same time without nesting?

For i = 1 To Number_of_Periods
For p = 1 To Number_of_Periods
ActiveCell.FormulaR1C1 = "=R7C3/2"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=R8C3/2"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=POWER(1+RC[-2], p)"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=RC[-5]/RC9"
ActiveCell.Offset(0, 1).Select
ActiveCell.FormulaR1C1 = "=RC[-5]/RC9"
ActiveCell.Offset(1, -6).Select
Next p, i

Thanks,

Rasberry
 
The reason I asked about the active cell is because you don't actually need to do all that selecting.

You could use something like this.
Code:
Set rng = ActiveCell
For I = 1 To Number_of_Periods - 1
    rng.FormulaR1C1 = "=R7C3/2"
    rng.Offset(0, 2).FormulaR1C1 = "=R8C3/2"
    rng.Offset(0, 4).FormulaR1C1 = "=POWER(1+RC[-2], " & I + 1 & " )"
    rng.Offset(0, 6).FormulaR1C1 = "=RC[-5]/RC9"
    rng.Offset(0, 7).FormulaR1C1 = "=RC[-5]/RC9"
    Set rng = rng.Offset(1, 0)
Next I
By the way I'm not 100% sure about the offsets but hopefully you get the idea.
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December

Forum statistics

Threads
1,215,583
Messages
6,125,664
Members
449,247
Latest member
wingedshoes

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