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

Noobest

New Member
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.

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
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
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
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.

Noobest

New Member
Thank you so much! Sorted

Fluff

MrExcel MVP, Moderator
Glad we could help & thanks for the feedback.

Replies
1
Views
187
Replies
13
Views
326
Replies
5
Views
126
Replies
6
Views
234
Replies
2
Views
185

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.

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

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