Excel User Group
Microsoft Excel blogs, forums, files. Read, ask questions, provide answers.

How Excel solves all my problems

Latest post Wed, May 14 2008 2:31 AM by kaak. 8 replies.
  • Thu, May 8 2008 2:25 PM

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 30
    • Points 545

    How Excel solves all my problems

    Last week I was on holliday and we had to divide 3 bottles of wine among ten friends.
    We could have written our names on little papers and draw 3 names from the lot,
    but why do something like that when you have a computer with excel.
    So I wrote a array function that returns a permutation from a set of data.

    I put the 10 names in A1:A10 and then selected C1:C3 and inserted =PERMUTATION(A1:A10)
    finally I pressed shift ctrl enter

    and voila ( sorry for the french ) a random permutation of 3 names…….

    the source for this small miracle is below

    Function PERMUTATION(ByRef rSource As Range) As Variant

       Dim PermCol As Collection: Dim Cell As Range: Dim Result() As Variant
       Dim iIndex As Long: Dim i As Long: Dim j As Long

       ReDim Result(1 To Application.Caller.Rows.Count, 1 To Application.Caller.Columns.Count)

       Set PermCol = New Collection

       i = 1

       For Each Cell In rSource

          PermCol.Add CStr(Trim(Cell.Value)), CStr(i): i = i + 1

       Next Cell

       For i = 1 To Application.Caller.Rows.Count

          For j = 1 To Application.Caller.Columns.Count

             iIndex = WorksheetFunction.RandBetween(1, PermCol.Count)
             Result(i, j) = PermCol(iIndex)
             PermCol.Remove (iIndex)

          Next j

       Next i

       PERMUTATION = Result

    End Function

    I don’t know any real life use for this function besides the dividing of winebotles I told you about.
    But if somebody has any use for this beautiful function please let me know…

    cheers

    Jelle-Jeroen

    • Post Points: 21
  • Thu, May 8 2008 3:22 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 234
    • Points 3,866
    • MVP

    Re: How Excel solves all my problems

    Kaak

    Well I like it a lot. I can think it is better that drawing straws and far more impressive.

    I'm marking that one as a favourite Yes

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    Filed under:
    • Post Points: 21
  • Thu, May 8 2008 4:39 PM In reply to

    Re: How Excel solves all my problems

     

    How about this, no VBA.

    First put all the names in a list, I named that range list_of_names.

    Then, ensure cell G1 is empty and goto Tools>Options and on the Calculation
    tab check the Iteration checkbox to stop the Circular Reference message.

    Next, type this formula into cell H1

    =IF(($G$1="")+(AND(H1>0,COUNTIF($H$1:$H$3,H1)=1)),H1,INT(RAND()*10+1))

    and this into I1

    =IF(H1<>0,INDEX(list_of_names,H1),"")

    Copy H1:I1 down to H3:I3.

    Now put something into G1, anything, an x will do, and watch the magic.

     

    To regnerate the list, clear G1, F2 H1 and copy down, then put something G1 again.

    Regards

    Bob

    • Post Points: 53
  • Fri, May 9 2008 11:02 AM In reply to

    Re: How Excel solves all my problems

     
    Very nice Bob.Yes

    Mike Alexander Microsoft Excel MVP www.datapigtechnologies.com
    • Post Points: 5
  • Fri, May 9 2008 5:57 PM In reply to

    • Nick Hodge
    • Top 10 Contributor
    • Joined on Sun, Dec 23 2007
    • Southampton
    • Posts 234
    • Points 3,866
    • MVP

    Re: How Excel solves all my problems

    Bob, as usual, I love it Yes

    Regards
    Nick Hodge
    Microsoft MVP, Excel
    Southampton, UK

    • Post Points: 5
  • Tue, May 13 2008 8:03 AM In reply to

    • Roger Govier
    • Top 10 Contributor
    • Joined on Wed, Jan 9 2008
    • Abergavenny, South Wales, UK
    • Posts 56
    • Points 950
    • MVP

    Re: How Excel solves all my problems

     very neat Bob!!
    I need a modification though, to ensure that my name always gets chosen to have a share of the wine!!
    I could put my name in each cell of of the list i supposeBig Smile

     

    Regards Roger Govier Microsoft Excel MVP
    • Post Points: 21
  • Tue, May 13 2008 1:04 PM In reply to

    Re: How Excel solves all my problems

    In H1, enter Roger.

    Then just put the original formula in H2:H3.

    Regards

    Bob

    • Post Points: 21
  • Tue, May 13 2008 7:39 PM In reply to

    • Roger Govier
    • Top 10 Contributor
    • Joined on Wed, Jan 9 2008
    • Abergavenny, South Wales, UK
    • Posts 56
    • Points 950
    • MVP

    Re: How Excel solves all my problems

     Too easy!!!!

    Regards Roger Govier Microsoft Excel MVP
    • Post Points: 21
  • Wed, May 14 2008 2:31 AM In reply to

    • kaak
    • Top 25 Contributor
    • Joined on Fri, Jan 18 2008
    • Edam, Netherlands
    • Posts 30
    • Points 545

    Re: How Excel solves all my problems

    Roger,

    Your name will appear in every permutation at a random spot...... 

     

    Function PERMUTATION(ByRef rSource As Range) As Variant

       Dim PermCol As Collection: Dim Cell As Range: Dim Result() As Variant
       Dim iIndex As Long: Dim i As Long: Dim j As Long

       ReDim Result(1 To Application.Caller.Rows.Count, 1 To Application.Caller.Columns.Count)

       Set PermCol = New Collection

       i = 1

       For Each Cell In rSource

          If Trim(Cell.Value) <> "Roger" Then PermCol.Add CStr(Trim(Cell.Value)), CStr(i): i = i + 1

       Next Cell

       For i = 1 To Application.Caller.Rows.Count

          For j = 1 To Application.Caller.Columns.Count

             iIndex = WorksheetFunction.RandBetween(1, PermCol.Count)
             Result(i, j) = PermCol(iIndex)
             PermCol.Remove (iIndex)

          Next j

       Next i
      
       Result(WorksheetFunction.RandBetween(1, Application.Caller.Rows.Count), WorksheetFunction.RandBetween(1, Application.Caller.Columns.Count)) = "Roger"

       PERMUTATION = Result

    End Function

    • Post Points: 5
Page 1 of 1 (9 items) | RSS
Copyright Excel User Group and the relevant contributors, 2008. All Trade Marks acknowledged. This site is a peer-to-peer site and NOT affiliated in any way to Microsoft Corp. All rights reserved.