Option Explicit
Sub testTwo()
Dim i As Integer, j As Integer
Dim temp As Variant
Dim deflt As String
Static elementsPerChoice As Integer
Static numChoicesMade As Integer
Static elementsRay As Range
Dim selectFrom As Variant
Dim randIndex As Integer, maxIndex As Integer
Dim writeRay As Range
Rem get elements
Rem input range
If Not (elementsRay Is Nothing) Then _
deflt = elementsRay.Address
On Error Resume Next
Set elementsRay = Application.InputBox _
("Select the elements to permute.", _
Default:=deflt, _
Type:=8)
If TypeName(elementsRay) <> "Range" Then Exit Sub
On Error GoTo 0
If Not (elementsRay.Rows.Count = 1 _
Or elementsRay.Columns.Count = 1) _
Then Beep: Exit Sub
Rem range to array
If elementsRay.Columns.Count = 1 Then
selectFrom = Application.Transpose(elementsRay)
Else
Rem array handeling for my environment
selectFrom = Application.Transpose(Application.Transpose(elementsRay))
End If
Rem how many elements per choice
If elementsPerChoice = 0 Then elementsPerChoice = 6
deflt = CStr(elementsPerChoice)
elementsPerChoice = Application.InputBox( _
"How many elements in each choice?", _
Default:=deflt, Type:=1)
If elementsPerChoice = False Then Exit Sub
If elementsPerChoice > elementsRay.Cells.Count Then _
elementsPerChoice = elementsRay.Cells.Count
Rem - - - - - - -Begin Edit - - - - - - - - - - - - - - -
Rem input how many to show and length of permiutation
If numChoicesMade = 0 Then numChoicesMade = 15
If elementsPerChoice = 0 Then elementsPerChoice = 6
deflt = CStr(numChoicesMade) & ", " & CStr(elementsPerChoice)
Do
temp = Application.InputBox( _
prompt:="Enter the number of choices to display and" _
& vbCrLf & vbTab & "the number of elements in each choice," _
& vbCrLf & vbTab & vbTab & "separated by a comma." _
& vbCrLf & vbCrLf & vbTab & "rows_displayed , elements_per_row", _
Default:=deflt, _
Type:=2)
If temp = "False" Then Exit Sub
On Error Resume Next
j = Application.Search(",", temp, 2)
On Error GoTo 0
Loop Until j > 0
numChoicesMade = Val(temp)
elementsPerChoice = Val(Mid(temp, j + 1))
If elementsPerChoice > elementsRay.Cells.Count Then _
elementsPerChoice = elementsRay.Cells.Count
Rem where to start writing
Set writeRay = ThisWorkbook.Sheets(1).Range("c2")
Set writeRay = Range(writeRay, writeRay.Cells(1, elementsPerChoice))
Rem - - - - - - begin New Edit - see post below - - - - - - -
Application.Calculation = xlManual
Rem - - - - - - - - - -End Edit - - - - - - - - - - - - - - - -
Rem main routine
For j = 1 To numChoicesMade
Rem randomly reorder selectFrom
maxIndex = UBound(selectFrom)
For i = 1 To elementsPerChoice
randIndex = Int(Rnd() * maxIndex) + 1
temp = selectFrom(randIndex)
selectFrom(randIndex) = selectFrom(i)
selectFrom(i) = temp
Rem maxIndex = maxIndex - 1 (not needed)
Next i
Rem show the first n elements of that re-ordering
writeRay.Offset(j - 1, 0).Value = selectFrom
Next j
End Sub