Thursday, 28 June 2018

MS Excel: Unique random number generator within a range


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?



Answer



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


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...