Create unique, random numbers in a column

ddub25

Well-known Member
Joined
Jan 11, 2007
Messages
617
Office Version
  1. 2019
Platform
  1. Windows
I would like the Item column of the Output Table (highlighted in GREEN) to return the value from the Item column in the Reference Table, where the corresponding row in the Rand column has a value of ‘1’.

The Rand column automatically generates random numbers from 1 to 4 in the rows where it matches the criteria from the Input Table.

The problem is at the minute, the random numbers are generated independent of each other and so I don’t get consecutive random numbers from 1 to 4 with each number being unique. It might randomly generate 2,2,3,4 for example, instead of 4,1,3,2 for example.

Is it possible to create a formula that can randomly generate each row with a unique random number from 1 to 4? I assume this might require some VB Code.

Can anyone help, Thanks. Dan

EXCEL POST_RandomNumGen_001.xlsx
ABCDEFGHIJKLMN
1
2INPUT TABLEOUTPUT TABLE
3Cat.STRCat.Lev.Loc.TypeItem
4Lev.2STR2HU2#N/A
5Loc.H
6TypeU2
7Count4
8
9REFERENCE TABLE
10Loc.
11Cat.Lev.HFTypeRandItem
12STR1FU1-Item 1
13STR2FU1-Item 2
14STR2HFU23Item 3
15STR3HFU2-Item 4
16STR2HFU23Item 5
17STR4HFU2-Item 6
18STR2HFU22Item 7
19STR3HFU2-Item 8
20STR2HFU3-Item 9
21STR2HFU3-Item 10
22STR1HFU3-Item 11
23STR2HFU3-Item 12
24STR1HFU3-Item 13
25STR2HU4-Item 14
26STR2HFU4-Item 15
27STR2HFU4-Item 16
28STR2HFU4-Item 17
29STR2HFU4-Item 18
30STR2HFU4-Item 19
31STR1HFU4-Item 20
32STR1HFU4-Item 21
33STR1HU2-Item 22
34STR1HU2-Item 23
35STR2HU22Item 24
36
Sheet1 (2)
Cell Formulas
RangeFormula
N4N4=INDEX(Table13[Item],MATCH(1,Table13[Rand],0))
G7G7=COUNTIFS($B12:$B35,G3,$C12:$C35,G4,$D12:$D35,G5,$F12:$F35,G6)
G12:G35G12=IF(AND($B12=G$3, $C12=G$4,$D12=G$5,$F12=G$6), RANDBETWEEN(1,G$7), "-")
Cells with Data Validation
CellAllowCriteria
J4List=CAT
K4List=LEV
L4List=LOC
G3List=CAT
G4List=LEV
G5List=LOC
G6List=TYPE
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Try...

G12, confirmed with CONTROL+SHIFT+ENTER, and copy down:

Excel Formula:
=IF(AND($B12=G$3,$C12=G$4,$D12=G$5,$F12=G$6),INDEX(AGGREGATE(15,6,ROW(INDIRECT("1:"&$G$7))/ISNA(MATCH(ROW(INDIRECT("1:"&$G$7)),$G$11:G11,0)),ROW(INDIRECT("1:"&$G$7-COUNT($G$11:G11)))),RANDBETWEEN(1,$G$7-COUNT($G$11:G11))), "-")

However, since the formula is resource intensive and volatile, a VBA solution might be better, as @James006 has suggested.

Hope this helps!
 
Upvote 0
Thanks for both of your suggestions - I'll look into both...
 
Upvote 0
I like the idea of going down the VBA route as eventually I would like to generate random numbers in the RAND column for all the other combinations of inputs in these rows and as you say Domenic, an excel formula would be resource intensive.

Initially, I would like the code to look at the different columns and IF Cat. = STR, IF Lev. = 2, IF Loc. = H, IF Type = U2, THEN realise the total number of columns that meet this criteria (4 columns meet it), THEN generate random numbers from 1 to 4 in the RAND column with NO DUPLICATES.

The code would be executed by a Command Button. When I click it, it regenerates the random numbers.

Can anyone help with some VBA code that would do that?
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,305
Members
449,079
Latest member
juggernaut24

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