How can I make the loop run all X and Y values at the same time, right now it runs all Y values and then all X values

Viewed 52

It runs through all the y values first, and then all the x values. I need X and y to change at the same time.

For x = 3 To lr + 1
    For y = 2 To lr
    anyl.Cells(x, 1) = efx.Cells(y, 1)
    Next y
Next x
2 Answers

Eliminate the inner loop (and y)

For x = 3 To lr + 1

    anyl.Cells(x, 1) = efx.Cells(x - 1, 1)

Next

The problem is that you designed the loops in a way that the outer loop starts the inner loop with every run. So you need to delete that inner loop, e.g.:

For x = 3 To lr + 1
    y = x - 1 // Or just change the value in the Cells-function* 
    anyl.Cells(x, 1) = efx.Cells(y, 1)
Next

*I prefer doing it this way, as you can easier reuse variable y.

Related