View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
STEVE BELL STEVE BELL is offline
external usenet poster
 
Posts: 692
Default Shuffle the cells!

I did something like this a long time ago.
The trick is to generate random numbers between 1 & 8 (or however many
columns)
Assume that the word list is on row 1 columns 1 - 8

You can generate an outer For Next loop that you can use to transfer the
results to multiple rows.
dim y as long
For y = 1 to 100

Next

This worked in Excel 2k. I repeated it about 50 times
and it looked fairly random...

Dim rand As Integer, x As Integer

For x = 1 To 8
Do Until Len(Cells(2, x)) 0
rand = Int((8 * Rnd) + 1)
If WorksheetFunction.CountIf(Rows(2), Cells(1, rand)) = 0 Then
Cells(2, x) = Cells(1, rand)
End If
Loop
Next

You can generate an outer For Next loop that you can use to transfer the
results to multiple rows.

dim y as long
For y = 2 to 100
For x = 1 To 8
Do Until Len(Cells(2, x)) 0
rand = Int((8 * Rnd) + 1)
If WorksheetFunction.CountIf(Rows(2), Cells(1, rand)) = 0 Then
Cells(y, x) = Cells(1, rand)
End If
Loop

Next

--
steveB

Remove "AYN" from email to respond
"M H" wrote in message
...
A question for interest:
The first row contains 8 words, one in each column. I want to shuffle
them randomly and display the results on the rows below. Presumably I
would expect to have 64 (8 x 8) combinations in this case. The problem
appears to be much difficult to be solved by VBA than I expected.
Furthermore, how to generalize the shuffling process for more words?

*** Sent via Developersdex http://www.developersdex.com ***