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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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

900
938
939
893
914
915
880
894
908
878
0
<COLGROUP><COL style="WIDTH: 54pt" width=72><TBODY> </TBODY>
 
Upvote 0

Forum statistics

Threads
1,215,835
Messages
6,127,169
Members
449,368
Latest member
JayHo

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