Random selections

alexv

New Member
Joined
Jun 26, 2006
Messages
26
I have a data in an excel sheet from column A to column L that is 10 rows long. I want to randomly pick data from 20 cells. I have searched the postings with not much luck. Any help?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,885
Hi alexv

Please try:

=INDEX($A$1:$L$10,1+INT(RAND()*10),1+INT(RAND()*12))

Hope this helps
PGC
 

alexv

New Member
Joined
Jun 26, 2006
Messages
26
Thank you.

I also would like for it to ignore the empty cells in the table so that my random sample will not include any blank values. Is there a way to do that?
 

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,885
Hi alexv

This is one possible solution.
This code writes in N1:N20 20 numbers taken randomly out of the non-empty cells in A1:L10

You can assign it to a Command Button to generate a different list each time you click on it.

Hope this helps
PGC

Code:
Sub PickRandNumbers()
Dim rCell As Range, dNumber(1 To 120) As Double, iMax As Integer, i As Integer

For Each rCell In Range("A1:L10")
    If rCell <> "" Then
        iMax = iMax + 1
        dNumber(iMax) = rCell
    End If
Next

For i = 1 To 20
    Range("N" & i) = dNumber(1 + Int(Rnd() * iMax))
Next
End Sub
 

Forum statistics

Threads
1,143,677
Messages
5,720,249
Members
422,272
Latest member
ginkgoVil

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
Top