Random number from Selection

Forestq

Active Member
Joined
May 9, 2010
Messages
482
Hi,

I want to select random value from selection. I have just:

Code:
Dim my_range()
Range("A2").CurrentRegion.Select
my_range = Selection

How to use Random function to select random value from my selection?
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Something like

Code:
Randomize
With Selection
    MsgBox .Item(Int(Rnd() * .Cells.Count)) + 1).Value
End With
 
Upvote 0
below code works:

Code:
Range("A2").CurrentRegion.Select
my_range = Selection
RandomDay = Int((UBound(my_range) * Rnd))
MsgBox Selection(RandomDay)
 
Upvote 0
Curious with the results I got with the following.
Where A2 to A12 = 1 through 11.
In column G2 to G12 =COUNTIF(F:F,1) to =COUNTIF(F:F,11)
The list below is the results of each formula.
Total entries in column F is 9060.
10,000 - 9060 = 940, so number 11 was chosen 940 times, but not entered in F column.

Regards,
Howard

Rich (BB code):
Option Explicit

Sub xx()

Dim myrg As Variant, i As Long
Dim RandomDay As Variant

Range("A2").CurrentRegion.Select
myrg = Selection

For i = 1 To 10000
RandomDay = Int((UBound(myrg) * Rnd))
'MsgBox Selection(RandomDay)
Range("F" & Rows.Count).End(xlUp).Offset(1, 0) = Selection(RandomDay)
Next

End Sub

[TABLE="width: 54"]
<COLGROUP><COL style="WIDTH: 54pt" width=72><TBODY>[TR]
[TD="width: 72, bgcolor: transparent, align: right"]900[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]938[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]939[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]893[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]914[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]915[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]880[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]894[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]908[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]878[/TD]
[/TR]
[TR]
[TD="bgcolor: transparent, align: right"]0[/TD]
[/TR]
</TBODY>[/TABLE]
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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