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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
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.
 
Upvote 0
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))
 
Upvote 0
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.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,357
Messages
6,124,483
Members
449,165
Latest member
ChipDude83

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