randbetween excluding multiple values

justinp2

New Member
Joined
Apr 18, 2013
Messages
3
Hello.

I've found a VBA script that will randomly select a value from within a given range, and will exclude a range of values.

I am trying to modify it to exclude two ranges of values, but am pretty lost. Ideally what it will do is return an integer from the given range "excluding" the values given in Exclude1 and Exclude2.

Could someone help me tweak this?

Code:
Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude1 As Range, Exclude2 As Range) As Long
Dim R As Long
Dim C As Range

Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In Exclude1
If R = C Then Exit For
Next C
Loop Until C Is Nothing

RandBetweenInt = R
Application.Volatile
End Function
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try something like:

Code:
Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude1 As Range, Exclude2 As Range) As Long
Dim R As Long
Dim C As Range
Dim JoinRange As Range

Set JoinRange = Union(Exclude1, Exclude2)


Do
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
For Each C In JoinRange
If R = C Then Exit For
Next C
Loop Until C Is Nothing
 
Upvote 0
To avoid the need to loop through two exclusion ranges this might be faster if those ranges are extensive.
Code:
Function RandBetweenInt(Lowest As Long, Highest As Long, Exclude1 As Range, Exclude2 As Range) As Long
Dim R As Long, n As Long
Do
n = n + 1
R = Lowest + Int(Rnd() * (Highest + 1 - Lowest))
If Application.CountIf(Exclude1, R) = 0 And _
    Application.CountIf(Exclude2, R) = 0 Then Exit Do
Loop Until n = 1000 'can remove the n=1000 part, just here to avoid if everything between lo & hi is excluded
RandBetweenInt = R
Application.Volatile
End Function
 
Upvote 0
Thank you both of these worked well.

I have a tangentially related question but will start a new thread.
Justin
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,488
Members
448,967
Latest member
visheshkotha

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