Random Selection Based on Criteria

FisheriesTech

New Member
Joined
Feb 3, 2022
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
I have a tricky question, which may be too complex/too much work for someone to bother answering, but I'll try anyways.
I'll use the below simple data set to try and illustrate my question.

In column A I have a unique Identifier and in Column B I have values that are associated with that Identifier. See below

1646176510433.png


What I want to do is pick one random value from column B for each unique value in column A and paste it into a separate column. For example there are 8 Alpha values in column A and each have an associated value in column B. I would like one of the column B values to be randomly selected for each unique value in column A and copied over to another column. I then need to repeat it for the other two Unique Values in column A. I currently have a VBA script written which uses an If statement to identify a unique value (for example the value 'Alpha') in column A and if it meets the criteria it copies the associated value from column B to another column. It then repeats the process for all of column A, populating a column of eight values (because there are eight cells in column A that contain the value 'Alpha'). The script then chooses a random value from there and copies it over to another column. This is great but I then have to write a whole new script to repeat the process for value Bravo and then for Delta and repeat the process. The catch is that in my real data set I have several hundred unique identifiers in column A. SO I would need to write several hundred scripts. Is there a way to ask Excel to go through column A, store each unique value as a 'Key' (without me having to identify each unique value) and then for each unique Key have Excel choose a random value associated with that key (would be one of the values from column B) and populate another column with these values. So if say I have a hundred unique Identifiers then I would end up with a hundred values from cell B associated with each of those unique identifiers but picked at random. The result would look something like below:
1646238655287.png
1646238665277.png

*Yellow cells *resulting random values
picked at random
for each identifier in
column A

I looked at using VBA dictionary but can't wrap my head around it. Maybe I'm asking for too much.
Any help appreciated.

Thank you kickass people out there!
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try this:

Book1
ABCDEF
1KeyValueKeyValue
2Alpha2Alpha2
3Bravo1Bravo8
4Delta4Delta4
5Alpha2  
6Bravo1  
7Delta0
8Alpha9
9Bravo4
10Delta7
11Alpha0
12Bravo2
13Delta4
14Alpha10
15Bravo9
16Delta8
17Alpha5
18Bravo6
19Delta2
20Alpha3
21Bravo8
22Delta7
23Alpha4
24Bravo2
25Delta1
Sheet10
Cell Formulas
RangeFormula
E2:E6E2=IFERROR(INDEX($A:$A,AGGREGATE(15,6,ROW($A$2:$A$25)/(COUNTIF($E$1:$E1,$A$2:$A$25)=0),1)),"")
F2:F6F2=IF(E2="","",AGGREGATE(15,6,$B$2:$B$25/($A$2:$A$25=E2),RANDBETWEEN(1,COUNTIF($A$2:$A$25,E2))))
 
Upvote 0
Solution
Try this:

Book1
ABCDEF
1KeyValueKeyValue
2Alpha2Alpha2
3Bravo1Bravo8
4Delta4Delta4
5Alpha2  
6Bravo1  
7Delta0
8Alpha9
9Bravo4
10Delta7
11Alpha0
12Bravo2
13Delta4
14Alpha10
15Bravo9
16Delta8
17Alpha5
18Bravo6
19Delta2
20Alpha3
21Bravo8
22Delta7
23Alpha4
24Bravo2
25Delta1
Sheet10
Cell Formulas
RangeFormula
E2:E6E2=IFERROR(INDEX($A:$A,AGGREGATE(15,6,ROW($A$2:$A$25)/(COUNTIF($E$1:$E1,$A$2:$A$25)=0),1)),"")
F2:F6F2=IF(E2="","",AGGREGATE(15,6,$B$2:$B$25/($A$2:$A$25=E2),RANDBETWEEN(1,COUNTIF($A$2:$A$25,E2))))

Oh my gosh!!! How?? It worked perfect. I have to hit the books to see what you did there. Such an elegant answer to the issue. Thank you so much. I didn't think it would be possible to be honest.
 
Upvote 0
Excel has lots of tricks like that! Nobody knows them all. Glad I could help, and thanks for the feedback! ?
 
Upvote 0

Forum statistics

Threads
1,223,239
Messages
6,170,947
Members
452,368
Latest member
jayp2104

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