• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Nested for loop, Cell print

I want to have a nested 'for' loop that prints the results to sequential horizontal cells. It would look something like the following:


for x = 150 to 200 increment by 50

for y = 2% to 6% increment by 1%

z = (some calculation involving x and y)

print y and z to cells (see NOTE: below)

endfor

endfor


NOTE:

As the inner is processed the print of y will occur and then z will be printed. These will be printed in the same row. Once the print occurs the row will be incremented then the next y,z print will occur. In the end the results will look similiar to the following:


A B

1 2% val

2 3% val

3 4% val...etc.
 
ok.. try something like this.. (may not work first time, but you can easily fix that)


range("A1").select


for x = 150 to 200

for y = 2 to 6

z = [calculation]

activecell.value = y

activecell.offset(0,1).range("a1").select

activecell.value = z

activecell.offset(1,-2).range("a1").select

next y

next x
 
or this

[pre]
Code:
Sub loopy()
Col = 1
Row = 1
For x = 150 To 200 Step 50
For y = 2 To 6 'Remember these are %
z = x * (y / 100) 'Change formula to suit
Cells(Row, Col) = y / 100
Cells(Row, Col).Style = "Percent"
Cells(Row, Col + 1) = z
Row = Row + 1
Next y
Next x
End Sub
[/pre]
 
Back
Top