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
 

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.
Rasberry

What are you actually trying to do with this code?

You don't even seem to be using the i variable anywhere and the p is in a text string so will probably be regarded as a named range by Excel.
 
Upvote 0
Yes, you are correct, when I use "P" it comes back with a name error. I'm trying to insert a power formula in a certain number of lines of excel (depending on number of periods) that is raised to a power each time. In Period 1 (cell 1), the interest rate is raised to the power "1", in period 2 (next cell), the interest rate is raised to the power "2", etc.
 
Upvote 0
I'm about to go offline but I'll ask this, why do you have a nested loop? You are trying to use 1 variable, p, but you don't seem to be using i at all.

Oh wait a minute I think I get it, your using the Select to move cells and using i to iterate that.

Are you sure you actually need to do that, couldn't you just copy the formula?
 
Upvote 0
I'll think about whether I can use the copy option. It works when I only use the "i". However, both the p and i need to be dynamic, I just want them to change at the same time. I'll keep thinking. Thanks for the suggestion about the copy option.
 
Upvote 0
Rasberry

You say you need the i to be dynamic but I don't see you actually using it in the code.

Maybe I'm missing something?:)
 
Upvote 0
Well, if I remove the "p" from the loop and the "p" from the formula and replace it with "1", the loop inserts the formula "i" times, where "i" is defined by Number_of_Periods (in the code prior to the piece I pasted in the message).
 
Upvote 0
cool--figured it out

My formula code was wrong. I always make this mistake. This following code works. (To your point that my mistake was causing the formula to be a string and the "i" was hardcoded as an i.)

For I = 1 To Number_of_Periods - 1
ActiveCell.FormulaR1C1 = "=R7C3/2"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=R8C3/2"
ActiveCell.Offset(0, 2).Select
ActiveCell.FormulaR1C1 = "=POWER(1+RC[-2], " & I + 1 & " )"
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 I
 
Upvote 0
When you run the code what's the activecell?
 
Upvote 0
The active cell is defined in the lines of code before this section of code. As the code goes through the loop, it inserts the formulas on the lines as instructed by the offset coding.
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,113,998
Members
448,539
Latest member
alex78

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