Generate a randum number

promoboy

New Member
Joined
Dec 5, 2010
Messages
19
I have following code and it does half the trick

when cell N2 = "divide" the randum number shuld only be dividable with cell value J2
When cell N2 is not "divide" the code should stay as it is

Code:
Public Sub generateRandNum1()
    'Define your variabiles
    Dim lgetal As String
    Dim hgetal As String
    lgetal = Worksheets("Blad 2").Range("f2").Value
    hgetal = Worksheets("Blad 2").Range("h2").Value
    lowerbound = lgetal
    upperbound = hgetal
    Set randomrange = Range("B9:B13")
    
    randomrange.Clear
    For Each rng1 In randomrange
        counter = counter + 1
    Next
    
    If counter > upperbound - lowerbound + 1 Then
        MsgBox ("Number of cells > number of unique random numbers")
        Exit Sub
    End If
    
    For Each Rng In randomrange
        randnum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
        Do While Application.WorksheetFunction.CountIf(randomrange, randnum) >= 1
            randnum = Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
        Loop
        Rng.Value = randnum
    Next
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
At the start define a ne variable I_multiplier

Set it to 1 or J2 depending on the value of N2


Then replace Rng.Value = randum with



Rng.Value = randum * I_multiplier
 
Upvote 0
At the start define a ne variable I_multiplier

Set it to 1 or J2 depending on the value of N2


Then replace Rng.Value = randum with



Rng.Value = randum * I_multiplier

Thank you that almost did the trick

The only problem i have now is that the ouput range isnt unique numbers, there are always numbers double.
 
Upvote 0
I missed that bit in your code ..... I think you need to apply the multiplier to the calculation of randnum instead of when you populate Rng
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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