RANDBETWEEN() excluding values

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

Does anybody know how to do a RANDBETWEEN(1,100) but have it exclude numbers that already exist within a defined range?

Thanks,
Jon
 
You can try this as well

Code:
=AGGREGATE(15,6,ROW($1:$100)/NOT(ISNUMBER(MATCH(ROW($1:$100),$A$1:$A$4,0))),RANDBETWEEN(1,100-COUNT($A$1:$A$4)))

Press Ctrl + Shift + Enter after putting the formula in cell as it is an array formula.

Where $A$1:$A$4 contains the numbers that needs to be excluded.
 
Upvote 0

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hi noj,

I don't know of any easy way to do this using RANDBETWEEN, so here is a custom user-defined function to make it easy:

Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude As Range) As Long
Dim R As Long
Dim C As Range
Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude
If R = C Then Exit For
Next C
Loop Until C Is Nothing

RandBetweenInt = R
Application.Volatile
End Function


To use this function, enter the cell formula

=RandBetweenInt(1,100,C1:C20)

where C1:C20 is the range containing the numbers you want to exclude.

To install this UDF in your workbook, go to the VBE (keyboard Alt-TMV), insert a new macro module (Alt-IM) and paste this code into the Code pane.

Damon
That really helped me. thanks Damon!
 
Upvote 0

Forum statistics

Threads
1,215,943
Messages
6,127,814
Members
449,409
Latest member
katiecolorado

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