Consulting

Results 1 to 4 of 4

Thread: Solved: VBA spiral

  1. #1
    VBAX Newbie
    Joined
    Oct 2010
    Posts
    4
    Location

    Solved: VBA spiral

    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.

    [VBA]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[/VBA]

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi theriot,

    The code could be made much more efficient by avoiding the selections:
    Sub Spiral()
    Dim userinput As Long, n As Long, i As Long, j As Long, rngAddr As Range
    userinput = InputBox("go how far?")
    n = 1: j = 0: i = 1
    Set rngAddr = ActiveCell
    Do Until n > userinput
      Do Until j > i
        If n <= userinput Then
          Set rngAddr = rngAddr.Offset(0, 1)
          rngAddr.Value = n
          rngAddr.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
          Set rngAddr = rngAddr.Offset(-1, 0)
          rngAddr.Value = n
          rngAddr.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
          Set rngAddr = rngAddr.Offset(0, -1)
          rngAddr.Value = n
          rngAddr.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
          Set rngAddr = rngAddr.Offset(1, 0)
          rngAddr.Value = n
          rngAddr.Interior.Color = vbBlue
          j = j + 1
          n = n + 1
        Else
          Exit Sub
        End If
      Loop
      i = i + 1
      j = 1
    Loop
    Set rngAddr = Nothing
    End Sub
    Note: You also need some error-checking in case the active cell doesn't allow enough room for the spiral range to grow.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Oct 2010
    Posts
    4
    Location
    Thank you. That was helpful. Our professor who teaches us this doesn't like to help us with our code or explain why anything is done.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    Then your professor is failing to fulfill their responcibility to "teach"
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •