Generate Random Number

Nick's picture

Random number generation VBA

This is the code to generate a random number within a range.. in this case 1 to 800.

Sub GenerateRandomNumberInRange()

MinNumber = 1
MaxNumber = 800
MsgBox Int((Rnd * (MaxNumber - MinNumber + 1)) + MinNumber)

End Sub

it will return an integer between 1 and 800.

Worksheet function

MsgBox Application.RandBetween(1, 800)


Nick's picture


nice, that's a better solution


Thats sweet! How would you go about making the result appear in a specific cell? For example, when you click on the command button, rather than the random number appearing in a dialogue box it will appear in say A4.