Generate random number from 40-80 but the numbers generated should end in either 5 or 0

Prabin Khanal

New Member
Joined
Oct 28, 2018
Messages
1
Hi Guys,

Can you help me out as I need to generate 7 numbers between 40-80 which should total 450 but the number must end with either 5 or zero-

I am unable to find it so if somebody can help me that would be very nice.

Cheers
Prabin
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
This is working reliably


Turn Iterations up. File > Options > Formulas > Maximum Iterations = 10,000

A1 =ROUNDDOWN(A2/5,0)*5
B1:G1 =ROUNDDOWN(RANDBETWEEN(40,80)/5,0)*5
H1 =SUM(A1:G1)

Then run:

Code:
Sub Macro1()

Dim x As Long


x = 1


Do Until x = 100
Application.StatusBar = x & " attempts out of 100"
If Range("A1") < 40 Or Range("A1") > 80 Or Range("H1") <> 450 Then
    Range("H1").GoalSeek Goal:=450, ChangingCell:=Range("A2")
End If


x = x + 1
Loop
    
End Sub

EDIT, looks to be working on the first try, maybe shorten the loop or remove.

Code:
Sub Macro1()

Dim x As Long


x = 1


Do Until x = 5
Application.StatusBar = x & " attempts out of 100"
If Range("A1") < 40 Or Range("A1") > 80 Or Range("H1") <> 450 Then
    Range("H1").GoalSeek Goal:=450, ChangingCell:=Range("A2")
End If


x = x + 1
Loop
    
End Sub


Code:
Sub Macro1()

    Range("H1").GoalSeek Goal:=450, ChangingCell:=Range("A2")

End Sub
 
Last edited:
Upvote 0
*Now not working on the first, either got very lucky there for a while or the first number wasn't truly random.

To rest you'd better clear A2
 
Upvote 0
Heres one that seems to work fine:

Code:
Dim arr(0 To 6)

Do Until Application.Sum(arr) = 450
    For i = 0 To 6
        Randomize
        arr(i) = Int((16 - 8 + 1) * Rnd + 8) * 5
    Next
Loop

MsgBox Join(arr, ", ")
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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