+ Reply to Thread
Results 1 to 8 of 8

Increasing cell value based on random test

  1. #1
    Registered User
    Join Date
    05-03-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    95

    Increasing cell value based on random test

    Hi

    I am creating my first bit of excel code to help me with some bookkeeping and so far so good. However, I have a question regarding the calculation of a cell's own value which I would appreciate some help with.

    For example, let's say I have a hand entered value in column A1 (for eg "11") which I want to increase by 1 point (to 12) on the outcome of a random event. In this instance, the value will increase by 1 if a random roll from 1 to 20 is higher than the current value, so on any roll of 12 or more. I know how to create a random number and I'm fairly sure of the logic to make the calculation, but I don't know how you make a cell "look" at itself and increment it's own data based on the logic outcome.

    Back in the mists of time using Amiga basic I'd do something like IF (random number)>A1 THEN A1=A1+1 but this is not an Amiga

    thanks
    Andy

  2. #2
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Increasing cell value based on random test

    You can do it with formulas. You would need to set the sheet to manual calculation so it does not recalculate every single time you do this.

    In B1 copied down, enter =IF(TRUNC(RAND()*(20)+1)>$A1,$A1+1,$A1)
    Last edited by Dulanic; 05-03-2010 at 10:03 AM.

  3. #3
    Registered User
    Join Date
    05-03-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Increasing cell value based on random test

    Thanks for the reply.

    Can you clarify a bit? When you say A1=11 B1 = etc etc are you saying I would need to spread the logic over three separate cells? Is it not possible to house all of the logic in A1?

  4. #4
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Increasing cell value based on random test

    All the logic can be done in B and copied down, it can not be A because A houses your value that you entered. So that can not be done with a formula inside A as that will just be overwritten. With formulas it would only be 2 cells, your values in A and then in B1 copied down it would be =IF(TRUNC(RAND()*(20)+1)>$A1,$A1+1,$A1) .

    If you want to keep it in A you could use VBA, see next post once I figured this out
    Last edited by Dulanic; 05-03-2010 at 11:12 AM.

  5. #5
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Increasing cell value based on random test

    Got it, this will do what you want by running the macro. That was so much easier than I remembered, just had to refresh my mind

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-03-2010
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    95

    Re: Increasing cell value based on random test

    Thank you for taking the time out to help me with this.

    I have to say I don't understand a single line of that code so I am just going to cut and paste it as it.

    Would you mind just telling me how I go about running a VBA script in a cell or could you point me to a faq that shows how it's done.

    many thanks again

  7. #7
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Increasing cell value based on random test

    Quote Originally Posted by AndyGW View Post
    Thank you for taking the time out to help me with this.

    I have to say I don't understand a single line of that code so I am just going to cut and paste it as it.

    Would you mind just telling me how I go about running a VBA script in a cell or could you point me to a faq that shows how it's done.

    many thanks again
    Easiest way.... press ALT + F11 which will open the VB editor. Under Insert, click module and paste that code. Then you can press ALT + F8 and select the macro and press run and you are done.

    Can see here for the basics of macros.

    http://www.excel-vba.com/excel-vba-s...-beginners.htm

  8. #8
    Forum Contributor
    Join Date
    12-03-2009
    Location
    Providence RI
    MS-Off Ver
    Excel 2007, Excel 2010
    Posts
    260

    Re: Increasing cell value based on random test

    Did you want to only do one cell at a time? Noticed you mentioned it being by cell... what I posted will process for all values in column A. If you want it to just do one cell at a time based on you selected cell this will work:

    Please Login or Register  to view this content.

    If you want to see what it is doing, this will tell you what the results were and what it did and will let you verify I entered this as you intended:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1