PDA

View Full Version : Copy random numbers and sort.



ashleygf
04-19-2015, 05:10 PM
Good Morning, I am attempting to develop a simple random draw spreadsheet for my local bowls club.
I would like help, if possible, with the following using Excel 2013.
In cells A1 .. A21 I will manually insert up to 21 random numbers (meaning any numbers up to 21 but not necessarily using all eg. omitting 3,5,10 19).
By means of a macro I would like to copy those numbers into say columns B and C. I would then like to sort all numbers so that A1 does not equal B1 or C1, A2 does not equal B2 or C2, A3 does not equal B3 or C3 etc.etc.
I have attempted the above but with no success and would appreciate any help.
Thanks and regards.

jonh
04-21-2015, 02:47 AM
Sub AllSorts()
Dim COLS As Byte: COLS = 2 '<-- number of columns to generate

Dim r As Range, c As Range, l As Long
If Len(Trim(Range("a1"))) = 0 Then Exit Sub
Set r = ActiveSheet.Range("a1:" & Range("a1").End(xlDown).Address)
l = r.Cells.Count
If COLS >= l Then COLS = l - 1
With r
For i = 1 To COLS
For Each c In r
If c.Row + i - l > 0 Then
Cells(c.Row + i - l, i + 1) = c.Text
Else
c.Offset(i, i) = c.Text
End If
Next
Next
End With
End Sub

ashleygf
04-21-2015, 03:57 AM
Thank you John. Very much appreciated.