Random Numbers (up to a max value) with no Duplicates - Draw Generator

Simon2001

New Member
Joined
Jun 28, 2019
Messages
16
Office Version
  1. 365
Platform
  1. Windows
Hello

This old post is quite frankly brilliant and exactly what I needed to create. I have been using a random generator but couldn't figure out how to avoid duplicates.


I've got it working perfect and I'm now trying to figure out how to make the range dynamic so rather than a fixed 90 rows/numbers, I want to be able to enter a value in a cell for maximum number of entries so 16, 32, whatever, etc)

Sub StartOver()
ActiveSheet.UsedRange.Clear
Range("E1") = "Number"
Range("F1") = "Sort"
Range("E2") = 1
Range("E2:E17").DataSeries Step:=1
Range("F2:F17").Formula = "=Rand()"
End Sub

Sub DrawUnique()
Range("F1").CurrentRegion.Sort Key1:=Range("F1"), Header:=xlYes
Range("E100").End(xlUp).Copy Destination:=Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)
Range("E100").End(xlUp).Resize(1, 2).Clear
End Sub

Can anyone suggest a way to alter this code to do this?

Thanks
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Thanks, I've just updated my profile. I've been away for so long!

Running Office 365 on Windows.
 
Upvote 0
Thanks for that, how about
VBA Code:
Sub Simon()
   Dim Ary As Variant
   Dim Rws As Long
   
   Rws = Range("B2").Value
   Range("A2:A10000").ClearContents
   Range("A2:A" & Rws + 1).Value = Evaluate("SORTBY(SEQUENCE(" & Rws & "),RANDARRAY(" & Rws & "))")
End Sub
Change B2 to the cell with the number of rows.
 
Upvote 0
Oh, wow, I don't even need the original code :) Thanks!

Is there anyway to get it so each time I run the code, it selects the next number rather than do them all in one go. Its to generate competition draws so Id like each number to come out one at a time up to the maximum value in B2
 
Upvote 0
You could use
VBA Code:
Sub Simon()
   Dim Ary As Variant
   Dim Rws As Long, i As Long
   
   Rws = Range("B2").Value
   Range("A2:A10000").ClearContents
   Ary = Evaluate("SORTBY(SEQUENCE(" & Rws & "),RANDARRAY(" & Rws & "))")
   
   For i = 1 To Rws
      Range("A1").Offset(i).Value = Ary(i, 1)
      Application.Wait (Now + TimeValue("00:00:05"))
   Next i
End Sub
which will output each number with a 5second delay between them
 
Upvote 0
Solution
PERFECT and thanks VERY much for the help with this. This works great!
 
Upvote 0
You're welcome & thansk for the feedback.
 
Upvote 0
Quick question, if for some reason I wanted to, could I get the next number to come by pressing enter or space bar rather than a time delay?
 
Upvote 0
Possibly, but it's not something I've ever done, so don't know how.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top