-
-
Save faeem81/ec9691e8393631f47165b970cda26878 to your computer and use it in GitHub Desktop.
Random Number Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Public Sub generateRandNum() | |
'Define your variabiles | |
lowerbound = Range("D22") | |
upperbound = Range("F22") | |
Set randomrange = Range("B24:F43") | |
randomrange.ClearContents | |
For Each rng1 In randomrange | |
counter = counter + 1 | |
Next | |
If counter > upperbound - lowerbound + 1 Then | |
MsgBox ("Number of cells > number of unique random numbers") | |
Exit Sub | |
End If | |
For Each Rng In randomrange | |
randnum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound) | |
Do While Application.WorksheetFunction.CountIf(randomrange, randnum) >= 1 | |
randnum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound) | |
Loop | |
Rng.Value = randnum | |
Next | |
End Sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment