Shuffle an Array

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good evening,

For what I am trying to do it appears that I need a Sub that "Shuffles" an array.
I have been surfing the web but unfortunately I can't find one that accommodates exactly what I am after. I basically want to be able to designate the size of the array (e.g. 34 numbers or 40 numbers or 45 numbers etc) and then choose whether I want them listed as 4 number combinations, 5 number combinations or 6 number combinations for example starting in cell "B2".

For example: If I was to use 34 numbers and produce 5 number combinations ...

combination 1 would be in cells B2:F2 ( 5 Numbers )
combination 2 would be in cells B3:F3 ( 5 Numbers )
...
combination 6 would be in cells B7:F7 ( 5 Numbers )
combination 7 would be in cells B8:E8 ( 4 Numbers )

If anyone has any ideas I would be grateful.
 
Last edited:
Mick,

It works like a dream, THANK YOU.
I have adapted it slightly for my specific needs and the revised code is ...

Code:
Sub MG09Sep08()
Dim Ray     As Variant
Dim num     As Integer
Dim n       As Integer
Dim txt     As String
Dim Rw      As Integer
Dim TxRay() As String
Dim c       As Integer
Dim rws     As Integer
Dim Col     As Integer
Dim ColNum  As Integer
Dim RwNum   As Integer
Dim Ac      As Integer
Dim Dn      As Integer
RwNum = Application.InputBox("How Many Numbers Would You Like To Shuffle?", "Randomize", Type:=1)
ColNum = Application.InputBox("How Many Numbers In Each Combination?", "Output", Type:=1)
Columns("A:K").ClearContents
'   Range("A1") = RwNum
'   Range("B1") = ColNum
Ray = Evaluate("row(1:" & RwNum & ") ")
rws = UBound(Ray, 1)
Randomize
ReDim nRay(1 To rws)
For Rw = 1 To rws
        c = 0
        If Rw = rws Then
            nRay(Rw) = Ray(1)
            Exit For
        Else
            num = Int(Rnd * UBound(Ray)) + 1
            nRay(Rw) = Ray(num, 1)
        End If
        For n = 1 To UBound(Ray, 1)
            If Not Ray(n, 1) = Ray(num, 1) Then
                ReDim Preserve TxRay(c)
                TxRay(c) = n
                c = c + 1
            End If
        Next n
        Ray = Application.Index(Ray, Application.Transpose(Array(TxRay)))
        Erase TxRay
Next Rw
Dn = 2
For n = 1 To UBound(nRay)
    Ac = Ac + 1
    If Ac = ColNum + 1 Then
        Dn = Dn + 1
        Ac = 1
    End If
        Cells(Dn, Ac + 1) = nRay(n)
Next n
End Sub

Thanks again.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.

Forum statistics

Threads
1,225,156
Messages
6,183,229
Members
453,152
Latest member
ChrisMd

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top