Mike H and Lars-Åke,
Many thanks for your macros - they both worked brilliantly and did exactly
what I asked for.
However, after running them, I realised I had come up with the wrong
solution to my problem - what I need is a random sequence not random
numbers.
The problem I am trying to address is table allocations for training events
for large numbers of people. I had assumed that a series of random numbers
say between 1 and 6 would, with a large enough number of delegates, give
almost equal numbers on each table. However, your macros showed me that it
does not. Using this method I ended up with sometimes double the number of
people on one table over another! On reflection, I guess this is bound to
happen with truly random numbers! How to learn the hard way!
So what would overcome this would be a macros which inserts say the numbers
1-6 (i.e. P2) in a random sequence and then continues with further random
sequences until it has covered P1 number of cells.
Is it possible to create a macro that will do this please?
Once again, many thanks,
V
PS This approach will also ensure that there is the widest possible mix of
people form all parts of the list on each table.
Here are two more.
This shorter one distributes the table numbers making sure that there
difference in number of participants per table is never more than one.
If the number of particpants is a multiple of the number of tables,
there will be the same number of participants on each table.
However, there is no guarantee that the first P1/P2 number of
participants on the list will not end up on the same table.
Sub victor_delta2()
Dim randoms() As Double
number_of_persons = ActiveSheet.Range("P1").Value
number_of_tables = ActiveSheet.Range("P2").Value
ReDim randoms(number_of_persons)
Randomize
For i = 0 To number_of_persons - 1
randoms(i) = Rnd()
Next i
For i = 0 To number_of_persons - 1
min_rand = 1
For j = 0 To number_of_persons - 1
If randoms(j) < min_rand Then
min_rand = randoms(j)
minj = j
End If
Next j
randoms(minj) = 1
ActiveCell.Offset(minj, 0) = (i Mod number_of_tables) + 1
Next i
End Sub
This longer one does the same, with the addition that there is a
guarantee that in the first, second, third, etc sequence of P2
participants on the list, they will be distributed on all P2 tables
(which is what you asked for I guess)
Sub victor_delta3()
Dim randoms() As Double
number_of_persons = ActiveSheet.Range("P1").Value
number_of_tables = ActiveSheet.Range("P2").Value
ReDim randoms(number_of_tables)
Randomize
base = 0
While base < number_of_persons
For i = 0 To number_of_tables - 1
randoms(i) = Rnd()
Next i
For i = base To base + number_of_tables - 1
min_rand = 1
For j = 0 To number_of_tables - 1
If randoms(j) < min_rand Then
min_rand = randoms(j)
minj = j
End If
Next j
randoms(minj) = 1
If base + minj < number_of_persons Then
ActiveCell.Offset(base + minj, 0) = (i Mod number_of_tables) + 1
End If
Next i
base = base + number_of_tables
Wend
End Sub
Hope this helps / Lars-Åke