Button to generate random values (with min/max) through range of cells

leebauman

Board Regular
Joined
Jul 1, 2004
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Can I create a button that produces random values through an array?

For example, I want a list of random numbers greater than 4 and less than 25 to appear in all cells A2:A10.

The numbers would remain until I press the button and then they'd re-randomize.

Thanks for considering!
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Assuming you want whole numbers only, put a button on the sheet and attach the following code to it:
VBA Code:
Sub Five_To_24()
    Dim a, b, i As Long, j As Long
    a = [ROW(5:24)]
    For i = UBound(a) To 2 Step -1
        j = Application.RandBetween(1, i)
        b = a(i, 1)
        a(i, 1) = a(j, 1)
        a(j, 1) = b
    Next i
    Range("A2:A10").Value = a
End Sub
 
Upvote 0
you can use
Excel Formula:
=RANDBETWEEN(4,25)
formula to do it, in vba, you can use:
VBA Code:
Sub RandomNum()
    For i = 2 To 10
        ActiveSheet.Cells(i, 1).Value = Int(4 + Rnd * 22)
    Next i
End Sub
 
Upvote 0
I want a list of random numbers greater than 4 and less than 25
@eiloken please note that both your suggestions can return both the number 4 and the number 25 which isn't exactly what the op asked for. Also, and it may not matter, but both your suggestions can lead to numbers being repeated, whereas the code in post #2 has no repeats in the list. As I said, that may not matter, up to the op's preference...
 
Upvote 0
@eiloken please note that both your suggestions can return both the number 4 and the number 25 which isn't exactly what the op asked for. Also, and it may not matter, but both your suggestions can lead to numbers being repeated, whereas the code in post #2 has no repeats in the list. As I said, that may not matter, up to the op's preference...
oh, i understand this now, thanks for your suggest
 
Upvote 0
I'm okay with values repeating. Also, a slight change...I need the output values to appear in C16:C380. I'm unable to figure out how to change the code so the values appear in that range rather than A2:A10 as I previously indicated.

Much appreciated!
 
Upvote 0
In that case, something like this might do
VBA Code:
Sub Rand5to24()
    With ActiveSheet.Range("C16:C380")
        .Formula = "=Randbetween(5,24)"
        .Value = .Value
    End With
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top