theriot
10-08-2010, 09:44 PM
Im trying to make a spiral in excel using a vba code. I managed to get it working but i think there may be a more efficient way to go about it. If anyone has any suggestions i would appreciate it.
Option Explicit
Sub spiral()
Dim userinput As Long
Dim n As Long
Dim i As Long
Dim j As Long
userinput = InputBox("go how far?")
n = 1
j = 0
i = 1
Do Until n > userinput
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(0, 1).Select
ActiveCell = n
ActiveCell.Interior.Color = vbRed
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(-1, 0).Select
ActiveCell = n
ActiveCell.Interior.Color = vbYellow
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(0, -1).Select
ActiveCell = n
ActiveCell.Interior.Color = vbGreen
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(1, 0).Select
ActiveCell = n
ActiveCell.Interior.Color = vbBlue
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Loop
End Sub
Option Explicit
Sub spiral()
Dim userinput As Long
Dim n As Long
Dim i As Long
Dim j As Long
userinput = InputBox("go how far?")
n = 1
j = 0
i = 1
Do Until n > userinput
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(0, 1).Select
ActiveCell = n
ActiveCell.Interior.Color = vbRed
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(-1, 0).Select
ActiveCell = n
ActiveCell.Interior.Color = vbYellow
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(0, -1).Select
ActiveCell = n
ActiveCell.Interior.Color = vbGreen
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Do Until j > i
If n <= userinput Then
ActiveCell.Offset(1, 0).Select
ActiveCell = n
ActiveCell.Interior.Color = vbBlue
j = j + 1
n = n + 1
Else
Exit Sub
End If
Loop
i = i + 1
j = 1
Loop
End Sub