MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Nested For Loop Plz Help

  Asked By: Pedro    Date: Sep 29    Category: MS Office    Views: 708

I am trying to change 2 varialbes at the same time. One variable is
used in a calculation(i) and another variable is used to define a
cell to paste the data in (j)

Here is my thought

Dim i = integer
Dim j = integer

For i = 440 to 600 step 5
For j = 1 to 500 step 1
Range ("A10").select
Formular R1C1 = (i)
'Copy range which is function of cell a10
Paste in a new sheet in column j
next j
next i

This does not do what I want, it loops through j for a given i.

So I want run case i, and paste the results in col j.
Then run case i+5,and paste in j+1
Then run cas i +10 and paste in j+2 etc.

I hope I have explained this well enough.



3 Answers Found

Answer #1    Answered By: Thomas Thompson     Answered On: Sep 29

I'm not sure I understand exactly what you are trying to do, but I think you
just need to increase j by one each pass through the loop  - I don't think you
need a nested  loop. Have you tried replacing inside loop with j = j+1?

Answer #2    Answered By: Morris Hughes     Answered On: Sep 29

How about something more like
For j = 1 to 500
i = 440 + (j - 1) * 5
' do your thing...

Doesn't seem to be quite what you want because j will run from 1 to 500,
while I will run from 440 to 2935, and you said you only need i to run
from 440 to 600, so I'm a little confused.

I agree with Doreen - you don't want two loops, just one, and then
calculate one value based on the other.

You can also try incrementing i at the same time  you increment j like

i = 440
For j = 1 to 500
' do your thing...
i = i + 5 ' now increment i

Maybe you can explain what you need to do a little more clearly?

Answer #3    Answered By: Leo Evans     Answered On: Sep 29

I just need to increment j in the i loop. I
fell kind of dumb after seeing this answer, but that is what
learning is all about!

Didn't find what you were looking for? Find more on Nested For Loop Plz Help Or get search suggestion and latest updates.