Randomly assign a range of numbers across the same number of cells

Noobest

New Member
Joined
May 12, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
I have written a script (my first) to generate the numbers 2-12, and assign these values in random order across a range of cells. My code works most of the time, but occasionally two instances of the same number occur. Please can someone help me improve my code? Thanks.

Row 1 and Column A are =RAND() and will be hidden.
Row 2 and Column B are as shown here. These should contain the values 2..12 in a random order.
As you can see, Column B has two 6s and no 9.
1620842536232.png

Help much appreciated, thanks.
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
Hi & welcome to MrExcel.
One way
+Fluff 1.xlsm
ABCDEFGHIJKLMN
1
241089115631227
39
410
52
612
76
83
98
107
114
125
1311
14
Record
Cell Formulas
RangeFormula
C2:M2C2=INDEX(UNIQUE(RANDARRAY(100,,2,12,1)),SEQUENCE(,11))
B3:B13B3=INDEX(UNIQUE(RANDARRAY(100,,2,12,1)),SEQUENCE(11))
Dynamic array formulas.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
Thinking about it, this is a simpler way
Excel Formula:
=SORTBY(SEQUENCE(11,,2),RANDARRAY(11))
and
Excel Formula:
=SORTBY(SEQUENCE(,11,2),RANDARRAY(,11))
 

Noobest

New Member
Joined
May 12, 2021
Messages
3
Office Version
  1. 2019
Platform
  1. Windows
Thank you :) I have tried both suggestions but I get Name Error. Quite possibly because I erred about my Excel version - its not 365, its Office Professional Plus 2019. Does that make a difference please? Or am I doing something incorrectly? Thanks again.
 

lrobbo314

Well-known Member
Joined
Jul 14, 2008
Messages
3,252
Office Version
  1. 365
Platform
  1. Windows
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
62,383
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Forum statistics

Threads
1,141,308
Messages
5,705,666
Members
421,403
Latest member
cmowla

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
Top