Random formula without clashing with each cell.

Kingkoopa

Board Regular
Joined
Aug 7, 2014
Messages
94
Hi Guys,

I am currently using windows 7 and excel 2010.

I have the following formula in Cell A & Cell B : RANDBETWEEN(0,5)

How do I make it so when I press the refresh button the random number chosen would not clash with each other?

Example :

I want to avoid having 0,0 1,1 2,2 3,3 4,4 5,5.

So if cell A generates the number 1. Cell B will not generate number 1. I think I have to combine with somekind of If formula?

Thank you
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
one way to do it


Excel 2013/2016
ABCDEF
10.0396820.5501380.3223840.9209480.3783050.07154
2
351
Sheet1
Cell Formulas
RangeFormula
A1=RAND()
A3=RANK(A1,$A$1:$F$1)-1
 
Upvote 0
Hi Alan,

It worked thanks! But any idea how to input them in VBA code?

My current code :
Code:
[Sub NumberGenerator()
Range("A3") = WorksheetFunction.RandBetween(0, 5)
Range("B3") = WorksheetFunction.RandBetween(0, 5)

Call test
End Sub

Sub test()
Scheduledtime = Now + TimeValue("00:00:10")
Application.OnTime Scheduledtime, "NumberGenerator"
End Sub

I tried modifying into :
Range("A3") = WorksheetFunction.RANK(A3,$A$1:$F$1)-1
Range("B3") = WorksheetFunction.RANK(B3,$A$1:$F$1)-1

But got an invalid error.
 
Last edited:
Upvote 0
Hi ,

Try this simple code :
Code:
Public Sub PickTwoRandomNumbers()
           With Me
                .[A3] = Int(Rnd() * 6)
                
                Randomize
                Do
                   .[B3] = Int(Rnd() * 6)
                Loop Until .[A3] <> .[B3]
           End With
End Sub
The code has to be placed in the worksheet section where you want the numbers placed ; if you place this in a code module , replace the keyword Me with the keyword ActiveSheet.
 
Last edited:
Upvote 0
This routine guarantees the two numbers are random between 0 and 5 and never equal to each other...
Code:
[table="width: 500"]
[tr]
	[td]Sub TwoNonEqualRandomNumbersBetween0and5()
  Dim Cnt As Long, RndIndx As Long, Tmp As Long, Arr As Variant
  Randomize
  Arr = [{0,1,2,3,4,5}]
  For Cnt = 6 To 1 Step -1
    RndIndx = Int(Cnt * Rnd) + 1
    Tmp = Arr(RndIndx)
    Arr(RndIndx) = Arr(Cnt)
    Arr(Cnt) = Tmp
  Next
  Range("A3:B3") = Evaluate("{" & Arr(1) & "," & Arr(2) & "}")
End Sub[/td]
[/tr]
[/table]
 
Upvote 0

Forum statistics

Threads
1,222,691
Messages
6,167,670
Members
452,131
Latest member
MichelleH77

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