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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,214,830
Messages
6,121,835
Members
449,051
Latest member
excelquestion515

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