0

I have the following code in VBA:

For n = 1 To 15

Cells(n, 8) = Application.Combin(2 * n, n)

next n

I want the n in the cells(n,8) to have an incerement 2, so the code skips a line after each entry.

Is it possible to have an other increment variable in this same loop that jumps 2 at once?

Thanks in advance!

1 Answer 1

1

EDIT: after reading the comment: I think what is needed is a counter to count, 1,2,3,4,5,6...15, and another one to count 1,3,5,7...15

For that, here is what is need to be done: basically, you want the first iterator to be a normal counter, and the second iterator to be odd numbers only.

So here is a simply input output table

input           output
-----           -----
1                1
2                3
3                5
4                7
5                9
6                11

From the above, we can deduce the formula needed to convert the input into the desired output: output = (input x 2) -1

And so, we can re-write our for loop to be like so:

For n=1 to 15
    Cells(n,8) = Application.Combin(2*n-1,n)
Next

============= End of Edit =========================

Simply, use the keyword STEP in the for loop

For n = 1 To 15 STEP 2   'STEP can also be negative
                         'but you have to reverse the starting, and endin
                         'value of the iterator

The values for n will be: 1, 3, 5, 7, 9, 11 , 13, 15

Alternatively, use a local variable inside the for loop for that purpose (in-case you want the loop to execute 15 times)

For n=1 to 15
    i = n + 1
    Cells(i,8) = Application.Combine(2*n,n)
Next
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I want the values for n to be 1,3,5,.. only in the cells(). In the combin, I want them to be 1,2,3,4,5,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.