Choose from a list "x" random no duplicates

Kishan

Well-known Member
Joined
Mar 15, 2011
Messages
1,648
Office Version
  1. 2010
Platform
  1. Windows
Using Excel 2000</SPAN></SPAN>
Hi,
</SPAN></SPAN>

VBA, which can choose "x random numbers" from a list of column A (list in the column a can be max 290 rows)
</SPAN></SPAN>

For example list shown in column A has 28 rows, I want "4 random (no duplicates), which I have placed in cell B1 and result in C2 down
</SPAN></SPAN>

Example data
</SPAN></SPAN>


Book1
ABCD
1List of Numbers4Rando Selected
20 | 0 | 0 | 0 | 0 | 0 | 20 | 0 | 0 | 0 | 2 | 0 | 0
30 | 0 | 0 | 0 | 0 | 1 | 10 | 0 | 1 | 0 | 0 | 1 | 0
40 | 0 | 0 | 0 | 0 | 2 | 00 | 1 | 0 | 0 | 0 | 1 | 0
50 | 0 | 0 | 0 | 1 | 0 | 11 | 1 | 0 | 0 | 0 | 0 | 0
60 | 0 | 0 | 0 | 1 | 1 | 0
70 | 0 | 0 | 0 | 2 | 0 | 0
80 | 0 | 0 | 1 | 0 | 0 | 1
90 | 0 | 0 | 1 | 0 | 1 | 0
100 | 0 | 0 | 1 | 1 | 0 | 0
110 | 0 | 0 | 2 | 0 | 0 | 0
120 | 0 | 1 | 0 | 0 | 0 | 1
130 | 0 | 1 | 0 | 0 | 1 | 0
140 | 0 | 1 | 0 | 1 | 0 | 0
150 | 0 | 1 | 1 | 0 | 0 | 0
160 | 0 | 2 | 0 | 0 | 0 | 0
170 | 1 | 0 | 0 | 0 | 0 | 1
180 | 1 | 0 | 0 | 0 | 1 | 0
190 | 1 | 0 | 0 | 1 | 0 | 0
200 | 1 | 0 | 1 | 0 | 0 | 0
210 | 1 | 1 | 0 | 0 | 0 | 0
220 | 2 | 0 | 0 | 0 | 0 | 0
231 | 0 | 0 | 0 | 0 | 0 | 1
241 | 0 | 0 | 0 | 0 | 1 | 0
251 | 0 | 0 | 0 | 1 | 0 | 0
261 | 0 | 0 | 1 | 0 | 0 | 0
271 | 0 | 1 | 0 | 0 | 0 | 0
281 | 1 | 0 | 0 | 0 | 0 | 0
292 | 0 | 0 | 0 | 0 | 0 | 0
30
31
32
Sheet1


Thank you in advance
</SPAN></SPAN>

Regards,
</SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Using Excel 2000VBA, which can choose "x random numbers" from a list of column A (list in the column a can be max 290 rows)
Will the values in each cell in Column A be unique or could there be duplicates within the list?
 
Upvote 0
A possible solution using formulas

Array formula in C2 copied down
=IF(ROWS(C$2:C2)>B$1,"",INDEX(A$2:A$29,SMALL(IF(ISNA(MATCH(A$2:A$29,C$1:C1,0)),ROW(A$2:A$29)-ROW(A$2)+1,0),RANDBETWEEN(1+SUM(COUNTIF(C$1:C1,A$2:A$29)),ROWS(A$2:A$29)))))
Ctrl+Shift+Enter

M.
 
Upvote 0
A possible solution using formulas

Array formula in C2 copied down
=IF(ROWS(C$2:C2)>B$1,"",INDEX(A$2:A$29,SMALL(IF(ISNA(MATCH(A$2:A$29,C$1:C1,0)),ROW(A$2:A$29)-ROW(A$2)+1,0),RANDBETWEEN(1+SUM(COUNTIF(C$1:C1,A$2:A$29)),ROWS(A$2:A$29)))))
Ctrl+Shift+Enter

M.
Hi Marcelo Branco, formula worked and gives the random as per cell value B also hitting F9 produce the new sets. But could it be a VBA solution? </SPAN></SPAN>

Thank you for your kind help
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0
Will the values in each cell in Column A be unique or could there be duplicates within the list?
Hi Rick, yes the values in each cell in Column A will be unique there will not be any single duplicates </SPAN></SPAN>

Thank you for asking a question to make a clarifying my request
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>

 
Last edited:
Upvote 0
Hi Rick, yes the values in each cell in Column A will be unique there will not be any single duplicates
Give this event code a try (it will automatically generate the list you want whenever you change the number in cell B1)...
Code:
[table="width: 500"]
[tr]
	[td]Private Sub Worksheet_Change(ByVal Target As Range)
  Dim Arr As Variant, Cnt As Long, RandomIndex As Long, Tmp As Variant
  If Target.Address(0, 0) = "B1" Then
    Range("C2", Cells(Rows.Count, "C").End(xlUp)).Offset(1).Clear
    Arr = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value
    For Cnt = UBound(Arr) To LBound(Arr) Step -1
      RandomIndex = Int((Cnt - LBound(Arr) + 1) * Rnd + LBound(Arr))
      Tmp = Arr(RandomIndex, 1)
      Arr(RandomIndex, 1) = Arr(Cnt, 1)
      Arr(Cnt, 1) = Tmp
    Next
    Range("C2").Resize(Range("B1").Value) = Arr
  End If
End Sub[/td]
[/tr]
[/table]

HOW TO INSTALL Event Code
------------------------------------
If you are new to event code procedures, they are easy to install. To install it, right-click the name tab at the bottom of the worksheet that is to have the functionality to be provided by the event code and select "View Code" from the popup menu that appears. This will open up the code window for that worksheet. Copy/Paste the event code into that code window. That's it... the code will now operate automatically when its particular event procedure is raised by an action you take on the worksheet itself. Note... if you are using XL2007 or above, make sure you save your file as an "Excel Macro-Enabled Workbook (*.xlsm) and answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.
 
Upvote 0
This is just another way.
I use the Dictionary object to make sure there are no duplicate results. Because even if the data (in col A) is unique, the 'randbetween' function can hit the same 'spot' more than once, generating duplicated results.

Code:
[COLOR=blue]Sub[/COLOR] a1075487a[B]()[/B]
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1075487-choose-list-x-random-no-duplicates.html[/COLOR][/I]
[COLOR=blue]Dim[/COLOR] i [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR][B],[/B] x [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR][B],[/B] n [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
[COLOR=blue]Dim[/COLOR] d [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR][B],[/B] va
 
va [B]=[/B] Range[B]([/B][COLOR=brown]"A2"[/COLOR][B],[/B] Cells[B]([/B]Rows.count[B],[/B] [COLOR=brown]"A"[/COLOR][B]).[/B][COLOR=blue]End[/COLOR][B]([/B]xlUp[B]))[/B]
[COLOR=blue]Set[/COLOR] d [B]=[/B] CreateObject[B]([/B][COLOR=brown]"scripting.dictionary"[/COLOR][B])[/B]
 
    [COLOR=blue]For[/COLOR] i [B]=[/B] [B][COLOR=crimson]1[/COLOR][/B] [COLOR=blue]To[/COLOR] UBound[B]([/B]va[B],[/B] [B][COLOR=crimson]1[/COLOR][/B][B])[/B]
    x [B]=[/B] WorksheetFunction.RandBetween[B]([/B][B][COLOR=crimson]1[/COLOR][/B][B],[/B] UBound[B]([/B]va[B],[/B] [B][COLOR=crimson]1[/COLOR][/B][B]))[/B]
    [I][COLOR=seagreen]'Debug.Print x[/COLOR][/I]
        [COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] d.Exists[B]([/B]va[B]([/B]x[B],[/B] [B][COLOR=crimson]1[/COLOR][/B][B]))[/B] [COLOR=blue]Then[/COLOR]
            n [B]=[/B] n [B]+[/B] [B][COLOR=crimson]1[/COLOR][/B]
            d[B]([/B]va[B]([/B]x[B],[/B] [B][COLOR=crimson]1[/COLOR][/B][B]))[/B] [B]=[/B] [COLOR=brown]""[/COLOR]
            [COLOR=blue]If[/COLOR] n [B]=[/B] [B][COLOR=crimson]4[/COLOR][/B] [COLOR=blue]Then[/COLOR] [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
    [COLOR=blue]Next[/COLOR]
    Range[B]([/B][COLOR=brown]"C2"[/COLOR][B]).[/B]Resize[B]([/B]d.count[B])[/B] [B]=[/B] Application.Transpose[B]([/B]d.Keys[B])[/B]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 
Upvote 0
Give this event code a try (it will automatically generate the list you want whenever you change the number in cell B1)...
Code:
[TABLE="width: 500"]
<TBODY>[TR]
[TD]Private Sub Worksheet_Change(ByVal Target As Range)
  End Sub
[/TD]
[/TR]
</TBODY>[/TABLE]
Hi Rick, thank you it worked as you said just changing the numbers in cell B1 generate x numbers i like the way it done. </SPAN></SPAN>

Would it be complicate to get macro please?
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Last edited:
Upvote 0
This is just another way.
I use the Dictionary object to make sure there are no duplicate results. Because even if the data (in col A) is unique, the 'randbetween' function can hit the same 'spot' more than once, generating duplicated results.

Code:
[COLOR=blue]Sub[/COLOR] a1075487a[B]()[/B]
[I][COLOR=seagreen]'https://www.mrexcel.com/forum/excel-questions/1075487-choose-list-x-random-no-duplicates.html[/COLOR][/I]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
Akuini, thank you for the macro solution, please can you check it highlights the line below in yellow and can get any results. I think the function does not work in excel 2000 may be I am wrong.</SPAN></SPAN>

Code:
</SPAN></SPAN>[COLOR=#000000]
x = WorksheetFunction.RandBetween(1, UBound(va, 1))[/COLOR]</SPAN></SPAN>[COLOR=#000000]
[/COLOR]
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan
</SPAN></SPAN>
 
Upvote 0
Akuini, thank you for the macro solution, please can you check it highlights the line below in yellow and can get any results. I think the function does not work in excel 2000 may be I am wrong.

Code:
[COLOR=#000000]
x = WorksheetFunction.RandBetween(1, UBound(va, 1))[/COLOR][COLOR=#000000]
[/COLOR]


Kind Regards,

Kishan


Yes, I think it won't work in Excel 2000.
Try changing this line:
x = WorksheetFunction.RandBetween(1, UBound(va, 1))
to this:
x = Int((UBound(va, 1)) * Rnd + 1)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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