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:
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)
Akuini, I am out of town so I do not have access to my desk computer one week, I will check it when I will go back and tell you the results

Thank you, have a nice weekend

Kind Regards
Kishan
 
Last edited:
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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)
Akuini, it worked perfect!! :beerchug:</SPAN></SPAN>

Thank you very much for your time and help
</SPAN></SPAN>

Have a nice weekend
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :)
</SPAN></SPAN>
 
Upvote 0
Akuini, it worked perfect!! :beerchug:

Thank you very much for your time and help


Have a nice weekend


Kind Regards,

Kishan :)


Actually there's a problem with my code. It doesn't guarantee that we will get 4 random number, it could be below 4. Well, if your data is big, say hundreds of rows, then the possibility to get the result below 4 is very small. But just to make sure that won't happen I modified the code a bit, so use this one instead:

Code:
Sub a1075487b()
'https://www.mrexcel.com/forum/excel-questions/1075487-choose-list-x-random-no-duplicates.html
Dim i As Long, x As Long, n As Long, z As Long
Dim d As Object, va

va = Range("A2", Cells(Rows.count, "A").End(xlUp))
Set d = CreateObject("scripting.dictionary")
z = 4  [COLOR=#0000ff]'change this to get different number of result[/COLOR]
If z > UBound(va) Then MsgBox ("Something wrong"): Exit Sub
    Do
    x = Int((UBound(va, 1)) * Rnd + 1)
        If Not d.Exists(va(x, 1)) Then
            n = n + 1
            d(va(x, 1)) = ""
        End If
    Loop Until n = z
    Range("C2").Resize(d.count) = Application.Transpose(d.Keys)
End Sub
 
Upvote 0
Actually there's a problem with my code. It doesn't guarantee that we will get 4 random number, it could be below 4. Well, if your data is big, say hundreds of rows, then the possibility to get the result below 4 is very small. But just to make sure that won't happen I modified the code a bit, so use this one instead:

Code:
Sub a1075487b()
'https://www.mrexcel.com/forum/excel-questions/1075487-choose-list-x-random-no-duplicates.html
Dim i As Long, x As Long, n As Long, z As Long
Dim d As Object, va

va = Range("A2", Cells(Rows.count, "A").End(xlUp))
Set d = CreateObject("scripting.dictionary")
z = 4  [COLOR=#0000ff]'change this to get different number of result[/COLOR]
If z > UBound(va) Then MsgBox ("Something wrong"): Exit Sub
    Do
    x = Int((UBound(va, 1)) * Rnd + 1)
        If Not d.Exists(va(x, 1)) Then
            n = n + 1
            d(va(x, 1)) = ""
        End If
    Loop Until n = z
    Range("C2").Resize(d.count) = Application.Transpose(d.Keys)
End Sub
Akuini, yes it correct, apart of that with the older code I found another problem too, say for example I filled data in column A with 266 rows and when I changed "z = 4" to "z = 266" it were generating only 165 random.</SPAN></SPAN>

But with this last modified code it generate 266 Perfectly, and if I changed "z = 267" gives message "Something wrong" now the code is running 100% accurate
</SPAN></SPAN>

Thank you Akuini, for rechecking the code thoroughly for me, I do appreciate your help a lot
</SPAN></SPAN>

Good Luck
</SPAN></SPAN>

Kind Regards,
</SPAN></SPAN>
Kishan :-D
</SPAN></SPAN>
 
Upvote 0
Akuini, yes it correct, apart of that with the older code I found another problem too, say for example I filled data in column A with 266 rows and when I changed "z = 4" to "z = 266" it were generating only 165 random.

But with this last modified code it generate 266 Perfectly, and if I changed "z = 267" gives message "Something wrong" now the code is running 100% accurate


Thank you Akuini, for rechecking the code thoroughly for me, I do appreciate your help a lot


Good Luck


Kind Regards,

Kishan :-D

You're welcome & glad it worked :)
 
Upvote 0
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.

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

I am sorry, but somehow I missed your request to make my event code into a macro. If you liked the way the event code worked, I am not sure why you wanted to replace it with a macro, but since you asked, here is the macro...
Code:
[table="width: 500"]
[tr]
	[td]Sub RandomCodeNumbers()
  Dim Arr As Variant, Cnt As Long, Idx As Long, Tmp As Variant
  Arr = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value
  If Range("B1").Value < 1 + UBound(Arr) Then
    Range("C2", Cells(Rows.Count, "C").End(xlUp)).Offset(1).Clear
    For Cnt = UBound(Arr) To 1 Step -1
      Idx = Int(Cnt * Rnd + 1)
      Tmp = Arr(Idx, 1)
      Arr(Idx, 1) = Arr(Cnt, 1)
      Arr(Cnt, 1) = Tmp
    Next
    Range("C2").Resize(Range("B1").Value) = Arr
  Else
    MsgBox "You asked for more random values than there are values to choose from!", vbCritical
  End If
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
I am sorry, but somehow I missed your request to make my event code into a macro. If you liked the way the event code worked, I am not sure why you wanted to replace it with a macro, but since you asked, here is the macro...
Code:
[TABLE="width: 500"]
<TBODY>[TR]
[TD]Sub RandomCodeNumbers()
  Dim Arr As Variant, Cnt As Long, Idx As Long, Tmp As Variant
  Arr = Range("A2", Cells(Rows.Count, "A").End(xlUp)).Value
  If Range("B1").Value < 1 + UBound(Arr) Then
    Range("C2", Cells(Rows.Count, "C").End(xlUp)).Offset(1).Clear
    For Cnt = UBound(Arr) To 1 Step -1
      Idx = Int(Cnt * Rnd + 1)
      Tmp = Arr(Idx, 1)
      Arr(Idx, 1) = Arr(Cnt, 1)
      Arr(Cnt, 1) = Tmp
    Next
    Range("C2").Resize(Range("B1").Value) = Arr
  Else
    MsgBox "You asked for more random values than there are values to choose from!", vbCritical
  End If
End Sub
[/TD]
[/TR]
</TBODY>[/TABLE]
Hi Rick, yes I liked the event code and it worked fine too, there is no any particular reason, just I am used to macros and find comfortable using them.

Thank you for the macro solution it worked perfectly!!
:beerchug:
I am extremely grateful to you, Rick, for your support.

Good Luck

Kind Regards,
Kishan
:-D
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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