View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
J.E. McGimpsey J.E. McGimpsey is offline
external usenet poster
 
Posts: 493
Default Randomize the order of the contents of an array

one way:

Public Sub RandomizeRange()
Dim temp As Variant
Dim arr As Variant
Dim rng As Range
Dim i As Integer, i1 As Integer
Dim j As Integer, j1 As Integer

Set rng = Range("A1:E5")
arr = rng.Value
For i = UBound(arr, 1) To 1& Step -1&
For j = UBound(arr, 2) To 1& Step -1&
i1 = Int(Rnd() * i) + 1&
j1 = Int(Rnd() * j) + 1&
temp = arr(i, j)
arr(i, j) = arr(i1, j1)
arr(i1, j1) = temp
Next j
Next i
rng.Value = arr
End Sub



In article ,
"Lee Wold" wrote:

I have a range of cells 5 * 5 (A1:E5) which have string contents in them at
present. I want to run a macro that randomly sorts the existing contents
into different cells - it has to be the order of the existing content that
is randomly re-ordered not the content itself. Think of it similar to a
bingo card with the contents being the text strings one, two,...., twenty
five - where I want to produce random outputs for printing. How would I do
this in VBA.

Cheers.