Consulting

Results 1 to 12 of 12

Thread: Fun Excel - Spiral through a range

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Fun Excel - Spiral through a range

    One of my professors told me about a job interview question that I thought was interesting:

    Given any 2d array, how would you loop through the array in a "spiral" format?

    I thought I would give it a try and I used Excel VBA because I can represent looping through the range of a worksheet visually by coloring which cell I'm in during the loop.

    So, if you have a 2d array of:

    [1][2][3][4]
    [5][6][7][8]
    [9][a][b][c]

    Looping through would look like:

    1, 2, 3, 4, 8, c, b, a, 9, 5, 6, 7


    See if you can solve it using VBA and excel. I attached my example, so don't open the code if you want to solve it yourself.

    Good luck and have fun!

    EDIT: Oh, and please attach a workbook and not your code example, so other people can try without the (easier) temptation to cheat!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    MS Excel MVP VBAX Mentor Andy Pope's Avatar
    Joined
    May 2004
    Location
    Essex, England
    Posts
    344
    Location
    Interesting puzzle, here's my attempt.
    Cheers
    Andy

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Nice solution Andy

    I wanted to do something like when I first attempted this, but I kept getting stuck. Now after seeing your code, it makes much more sense!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    I thought this'd be a good one for trying recursion (a sub calling itself). This worked, but when testing on larger ranges ended up with a stack error, and a few problems with ranges on the edges of the sheet, so I abandoned it (it's included in the attached).

    However, the second macro (test2) works on any size range, anywhere.

    Pascal
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  5. #5
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Similar to OP's.

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hey Joseph,

    Quite the brain-teaser for me leastwise. (Especially when I think I read the "goal" wrong initially. )

    Anyways, hopefully one of the two ways is 'on the mark'.

    Have a good day,

    Mark

  7. #7
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by p45cal
    I thought this'd be a good one for trying recursion (a sub calling itself). This worked, but when testing on larger ranges ended up with a stack error, and a few problems with ranges on the edges of the sheet, so I abandoned it.
    Pascal
    This one does use recursion without the drawbacks mentioned earlier and has been tidied up.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location

    Cool

    Quote Originally Posted by p45cal
    This one does use recursion without the drawbacks mentioned earlier and has been tidied up.
    @p45cal:

    Greetings Pascal,

    Just wanted to mention as to: Nice recursion! Impressive and efficient :-)

    @malik641

    Hey Joseph,

    After submitting first try, got to look at others, including your example; only then realizing (probably/maybe?) what was supposed to be done.

    Not particularly efficient, but shucks, w/three different ways, one has to be correct, right?

    This was an interesting exercise - thank you so much!

    Mark

  9. #9
    Evening Everyone.

    Well Mark, third time's the charm, right? It looks like that last spiral matches the description of the project. I must admit, though, the coloring you've added is a very nice touch. Hmm. Maybe I have gamers disease. I like the bright, colorful flashes. It's mesmerizing. I'll have to modify the code to get it to just loop, and loop, and loop...

    P45Cal, that was a truly impressive display you put on there. My only comment is that you may want to add a slight delay in your loop. In was just a blur on my machine. That or I'll have to go borrow Mark's Flintstonian laptop. :-)


    Now let's see you do it backwards...

    Take care guys.
    Scott
    You don't understand anything until you learn it more than one way. ~Marvin Minsky

    I never teach my pupils; I only attempt to provide the conditions in which they can learn. - Albert Einstein

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Now let's see you do it backwards...
    Very funny and not a chance. My poor eyes were going blurry as was, and the laptop... I actually thought I heard whimpering.

  11. #11
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Demosthine

    Now let's see you do it backwards...

    Scott
    like this?

    ps. no delay put in, but do it on a big enough range..
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  12. #12
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    I know it's been over a month, but I just want to say that I loved everyone's solution! I hope you all had fun I most especially like the spiral backwards code by p45cal nice work!

    I just wanted to pay my respects. I have been entrenched with so much homework that I haven't had time for fun with VBAX.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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