Get Random Selection

joand

Active Member
Joined
Sep 18, 2003
Messages
266
In Column A, I have a list of Item Types, e.g. Item 1, Item 2, Item 3 in unsorted order. I need to randomly select 5% from each Item type and output the randomly selected items in column B. Is there a simpler way to do this?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Here's an illustration. In column A are the item types. In Column B are the specific/unique item codes. I would like to output the random selection in column C. Since item 1, item 2 and item 3 have 20 counts each. 5% of each is 1, which means I need to select 1 from each item type randomly.

Column A Column B Column C
Item 1 Train 1 Train 3
Item 1 Train 2 Car 1
Item 1 Train 3 Boat 11
Item 1 Train 4
Item 1 Train 5
Item 1 Train 6
Item 1 Train 7
Item 1 Train 8
Item 1 Train 9
Item 1 Train 10
Item 1 Train 11
Item 1 Train 12
Item 1 Train 13
Item 1 Train 14
Item 1 Train 15
Item 1 Train 16
Item 1 Train 17
Item 1 Train 18
Item 1 Train 19
Item 1 Train 20
Item 2 Car 1
Item 2 Car 2
Item 2 Car 3
Item 2 Car 4
Item 2 Car 5
Item 2 Car 6
Item 2 Car 7
Item 2 Car 8
Item 2 Car 9
Item 2 Car 10
Item 2 Car 11
Item 2 Car 12
Item 2 Car 13
Item 2 Car 14
Item 2 Car 15
Item 2 Car 16
Item 2 Car 17
Item 2 Car 18
Item 2 Car 19
Item 2 Car 20
Item 3 Boat 1
Item 3 Boat 2
Item 3 Boat 3
Item 3 Boat 4
Item 3 Boat 5
Item 3 Boat 6
Item 3 Boat 7
Item 3 Boat 8
Item 3 Boat 9
Item 3 Boat 10
Item 3 Boat 11
Item 3 Boat 12
Item 3 Boat 13
Item 3 Boat 14
Item 3 Boat 15
Item 3 Boat 16
Item 3 Boat 17
Item 3 Boat 18
Item 3 Boat 19
Item 3 Boat 20
 
Upvote 0
paste this code into a module

Code:
Public Sub Random5Pct()
Dim colNames As New Collection
Dim i As Integer, iCnt As Integer, iNum As Integer, iCol As Integer
Dim vPicks
Const kPICKpct = 5


On Error GoTo errRand


     'load the collection
Sheets("names2").Select
Range("A2").Select
While ActiveCell.Value <> ""
   colNames.Add ActiveCell.Value
   ActiveCell.Offset(1, 0).Select   'next row
Wend


    'get the % to randomize
vPicks = kPICKpct / 100 * colNames.Count


'Sheets.Add
 Range("B1").Value = "Picks"
 Range("B2").Select


    For i = 1 To vPicks
      iCnt = colNames.Count
      'iNum = Int(iCnt * Rnd) + 1
      iNum = Int(1 + Rnd() * (iCnt - 1 + 1))
      
      ActiveCell.Value = colNames(iNum)
      colNames.Remove iNum
      
       ActiveCell.Offset(1, 0).Select   'next row
    Next
   
set colNames = nothing
errRand:
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,109
Messages
6,128,875
Members
449,476
Latest member
pranjal9

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