Hello Everyone,
First off, thanks for looking at this post. I'm relatively new to coding in VBA and am starting to get a little bit of it. But, I have this random number generator (I didn't code it) that I've been using. Unfortunately, it's been crashing when I've been trying to modify it. I think the chance of it crashing increases as the range of random numbers decrease in proportion to the amount of numbers you want to generate increases.
In any event, here's what I would like the code to do:
1. List numbers 1-50
2. List all 50 numbers random
3. No repeats.
Below is the code:
Option Explicit
Sub Random()
Dim x As Long, y As Long, z As Long, tempnum As Long
Dim flag As Boolean
Dim i As Integer
Dim foundCell As Range
Application.ScreenUpdating = False
x = Application.InputBox("Enter starting Random Number", "Random Number Generation", 1, , , , , 1)
y = Application.InputBox("Enter ending Random Number", "Random Number Generation", 1000, , , , , 1)
z = Application.InputBox("How many random numbers would" & "you like to generate (<15000)?", "Random Number Generation", 100, , , , , 1)
If z = 0 Then Exit Sub
If z > 15000 Then z = 15000
If z > y - x + 1 Then
MsgBox "You specified more numbers to return than " & "are possible in the range!"
Exit Sub
End If
Randomize
Cells(1, 1) = Int((y - x + 1) * Rnd + x)
For i = 2 To z
Do
flag = False
Randomize
tempnum = Int((y - x + 1) * Rnd + x)
Set foundCell = Range("a1", Range("a1").End(xlDown).Address).Find(tempnum)
If Not (foundCell Is Nothing) Then
flag = True
End If
Loop Until Not flag
Cells(i, 1) = tempnum
Next
End Sub
Bookmarks