Mike
Mike

Reputation: 2313

Random values for a range

I have the following code:

Low = 10
High = 25
R = Int((High - Low + 1) * Rnd() + Low)

Sheets("ITEMS DATA").Range("D4:D39").Value = R

I would like for the range stated to return a random number, but it returns the same random number for the whole range (D4:D39). I would like to return a different random number for the range stated but don't know how.

Upvotes: 0

Views: 344

Answers (2)

Jubbles
Jubbles

Reputation: 4570

@Mike:

Try this:

Low = 10
High = 25
For c = 4 To 39
    R = Int((High - Low + 1) * Rnd() + Low)
    Sheets("ITEMS DATA").Cells(c, 4).Value = R
Next

Of course, you can change the two arguments in the Cells() method to your liking.

Upvotes: 2

Pete
Pete

Reputation: 1303

You have created one random value, assigned it to R, and then put that value into your range. There are a few ways you can do what you want to do. I'm assuming there is a good reason not to just paste the formula into the cells directly, so:

    Range("B1:B21").Formula = "=Int((A1 - A2)+1) * Rand() + A2"

would do it, assuming that A1 and A2 hold High and Low respectively.

Edited to remove wrong answers, and this one has been tested.

Upvotes: 1

Related Questions