I just started learning to programming in Excel VBA, and I have a problem.
I want to draw a spiral with a program, but a can not solve it well.
I would like to use for
and do until
and do while
cycles only.
Here is my code:
Sub spiral()
For i = 1 To 16
xmm = xmm - 1
ymm = ymm - 1
Do Until xp = i
xp = xp + 1
Cells(5 + xp, 5) = i
Loop
Do Until yp = i
yp = yp + 1
Cells(5 + xp, 5 + yp) = i + 1
Loop
Do Until xm > xp
xm = xm + 1
Cells(5 + xp + i * xmm, 5 + yp) = i + 2
Loop
Do Until ym > yp
ym = ym + 1
Cells(5 + xp + xmm, 5 + yp + i * ymm) = i + 3
Loop
Next i
End Sub
Thanks!
Base on some answers from this thread (Looping in a spiral), I've managed to come with this solution
Sub spiral()
x = 0
y = 0
d = 1
m = 1
i = 1
j = 1
Do While i < 6
Do While 2 * x * d < m
Cells(x + 5, y + 5).Value = j
x = x + d
j = j + 1
Loop
Do While 2 * y * d < m
Cells(x + 5, y + 5).Value = j
y = y + d
j = j + 1
Loop
d = -1 * d
m = m + 1
i = i + 1
Loop
End Sub
Not sure if it is something that you are looking for thou, as you didn't specify the exact output that you want to achieve.