MS Excel: Unique random number generator within a range

0

I searched for a similar question but couldn't find one..

I want to generate 10 groups out of numbers ranging from 1-60 (including both), with each and every group containing random and non-repeating numbers. How can I do this in excel?

user235376

Posted 2013-07-03T07:53:42.860

Reputation:

These may be helpful: http://stackoverflow.com/q/5753063/657668 http://superuser.com/q/277386/76571

– Excellll – 2013-07-03T14:01:12.813

Answers

1

How to use it

  1. Open Excel & VBA editor (Alt+F11)
  2. Insert the code below under Sheet1
  3. Go back to Excel and select your desired range to fill with random & non-repeating numbers
  4. Execute the macro (Alt+F8)

Sub randomNumbers()
    Low = Application.InputBox("Enter first valid value", Type:=1)
    High = Application.InputBox("Enter last valid value", Type:=1)
    Selection.Clear
    For Each cell In Selection.Cells
        If WorksheetFunction.CountA(Selection) = (High - Low + 1) Then Exit For
        Do
            rndNumber = Int((High - Low + 1) * Rnd() + Low)
        Loop Until Selection.Cells.Find(rndNumber, LookIn:=xlValues, lookat:=xlWhole) Is Nothing
        cell.Value = rndNumber
    Next
End Sub

Excel file to proof

I love those small and simple solutions so much

nixda

Posted 2013-07-03T07:53:42.860

Reputation: 23 233

Its maybe possible wit Excel functions only: =RANDBETWEEN(1,60) and maybe an array function? But I have no clue how to check for duplicates. – nixda – 2013-07-03T11:22:36.887

1http://superuser.com/q/277386/76571 – Excellll – 2013-07-03T14:02:49.693

Well, as you already mentioned in the comments, there's a small downside because of MOD. But its a good solution anyway. – nixda – 2013-07-03T14:37:10.517

I tried the above mentioned steps but while executing, 9 cells are not filled and and then it crashes.. – None – 2013-07-03T16:21:28.760

I selected 6 x 10 cells (rows or columns) , my first value being 1 and end value being 60. A1 to F10 – None – 2013-07-03T17:24:45.693

Your workbook is showing a flawless execution but I followed the exact same steps as given by you. What were the changes that you made? – None – 2013-07-03T19:18:02.603

You can click on "edited x hour ago" under my post to see a history. I changed Selection.Cells.Find(rndNumber) to Selection.Cells.Find(rndNumber, LookIn:=xlValues, lookat:=xlWhole). I guess, everything is working for you now? – nixda – 2013-07-03T19:19:37.087

Thank you very much! Could you please suggest links or books where I can learn VBA scripting for excel right from the fundamentals? I want learn it but don't know where to start..' – None – 2013-07-03T19:26:05.367

Start here for a beginner guide. Use this if you need more infos to a specific function.

– nixda – 2013-07-03T19:35:53.157

1

I thought I had an answer, but this was a dead end (but quite fun) and I don't know how to remove it. My need is to do bingo-sheets with random setups.

Make a column A4:A63 with the numbers 1 to 60. Enter a fairly big prime number less than 60 cell B1. For example '47`.

In B2 enter

=RANDBETWEEN(1;59)

In rows B4:B63 fill with

=MOD(((A4+B$2)*B$1);60)+1 
=MOD(((A5+B$2)*B$1);60)+1 
...
=MOD(((A63+B$2)*B$1);60)+1 

This will make a fairly random sequence. B$2 will only shift the sequence.

Grubbis

Posted 2013-07-03T07:53:42.860

Reputation: 11