Skip to content

Instantly share code, notes, and snippets.

@faeem81
Created August 22, 2021 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faeem81/ec9691e8393631f47165b970cda26878 to your computer and use it in GitHub Desktop.
Save faeem81/ec9691e8393631f47165b970cda26878 to your computer and use it in GitHub Desktop.
Random Number Generator
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