Macro help - Compare random numers and add

Ellias_Adelle

New Member
Joined
Sep 13, 2021
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm pretty new to working with macros, so I hope I'm sorry if this seems easy to you. It has been stumping me for days and I can't figure it out no matter how much research I do.

What I am trying to do is compare a random number between 1 and 100 (no decimals) to a value in a cell and if that value is above that number I want to permanently add a random number (no decimals) between 1 and 10 to that number.

For example, if the value is 70 I would like the macro to add a random number between 1 and 10 to it 30 percent of the time. Let's say the macro increases the number to 76. If activated again I want it to have a 24 percent chance of increasing the number again.

If there is an easier way to do this than creating a random number and comparing it to the value I'm fine with that. I so long as the results are the same.

I don't want to have a value for the random numbers be posted in the workbook.
 
It is only hard until you familirize yourself :) Once you understand the object model it will be much easier.
 
Upvote 0

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Here is another way of applying the same logic

VBA Code:
Sub AddNum()
  Randomize
  With Range("A1")
    .Value = .Value + IIf(1 + Int(Rnd() * 100) > .Value, 1 + Int(Rnd() * 10), 0)
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,032
Messages
6,122,772
Members
449,095
Latest member
m_smith_solihull

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