Excel: Generate a random integer within a given range in VBA

The other day I needed to write a bit of VBA code that involved the random selection of an integer between two numbers. Let's say you want to generate a random integer between 500 and 800. Here's how to make that happen with VBA, storing it in a variable I'm calling randomNumber:

Dim randomNumber As Integer
Randomize
randomNumber = Int((800-500+1) * Rnd + 500)

0 comments:

Post a Comment