+ Reply to Thread
Results 1 to 15 of 15

Get Random amounts depending on a cell value

  1. #1
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Get Random amounts depending on a cell value

    Hello Experts
    There are various amounts in column E starting from 0. The total of the amounts is displayed in the end. I want to replace the amounts with the help of a code button to get the total of amount mentioned in cell HI without missing any row and the amounts should be greater than 400 and less than 9950.
    This is a sample data and the amount may change in cell H1 as per requirement. Amount can be anything in column H1 but is always less than the total amount in the end.
    I am sharing the previous similar code of beyond excel for reference assuming that it is possible to do so. As there were no comments in the code I was not able to edit the code and do the needful.
    I would appreciate if someone could help me with a code button to get the expected result ias displayed n column F. (not necessarily to get the same figures but the total should match).
    Thank you in advance.
    Last edited by RAJESH SHAH; 04-23-2023 at 10:24 PM. Reason: #Solved by Marc L
    "There is nothing called problem, it's just absence of an idea to find*solution."
    ?I have not failed. I?ve just found 10,000 ways that won?t work.?
    ?Thomas Edison

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this !


    According to your attachment a VBA demonstration as a beginner starter
    (as a reminder all is explained in both Excel & VBA helps) :

    PHP Code: 
    Sub Demo1()
        
    Dim L&, M%, N&(), R&, Z&
            
    = [E1].End(xlDown).Row
            M 
    9
            ReDim N
    (2 To L0)
        
    With Application
        
    Do
            For 
    2 To L:  N(R0) = .MRound(.RandBetween(4509900), 50):  Next
            Z 
    = .Sum(N) - [H1]
         If 
    0 Then
         
    For L To 2 Step -1
            
    If N(R0) - >= 450 Then
                N
    (R0) = N(R0) - Z
                Z 
    0
                
    Exit Do
            ElseIf 
    N(R0) > 4200 Then
                Z 
    N(R0)
                
    N(R0) = .MRound(N(R0) / 1050)
                
    N(R0)
            
    End If
         
    Next
         End 
    If
            
    1
        Loop 
    While Z
        End With
            
    If Z Then Beep Else Range("E2:E" L) = N
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 04-23-2023 at 09:34 PM. Reason: typo ...

  3. #3
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Hello, try this !

    Marc L. Awesome. Thank you very much. Comments could have really helped me to edit and understand better. Anyways, will try and see if I can play with it. Thanks once again.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get Random amounts depending on a cell value


    First thanks for the rep' !

    Again as all is explained in Excel & VBA respective help so you can understand it, the best way to at least start to learn !

    Just note in some case if after the Max 9 attempts this demonstration can't find a matching result
    so you will have to code the part for Z < 0 if only H1 and bounds can match with the number of rows …

  5. #5
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    Yes. I was facing an issue when I changed the amount in H1. Testing it with less and more rows and different amounts. I am hearing a beeping sound. The rows can range from 25 to 500 rows and the amount as I said in column H1 will be less than the sum total of column E. Still trying to understand the code ...

  6. #6
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    I understood this part does check the difference between the total sum and the value of H1. If the difference is >0 then the code will display the result.
    Z = .Sum(N) - [H1]
    If Z > 0 Then

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get Random amounts depending on a cell value


    No it can't display the result as it must first try to reduce the sum (the issue when random can't be just random !)
    then displays the result only when it succeeds …

  8. #8
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    It is perfect Marc. I have been doing this manually for years now. I have to change the amount in H1 as per my requirement which will be always less than the total sum of column E. In each scenario only the number of rows and the figure in H1 will be changed. It is as I expected. I really appreciate your time and effort for helping me with the code. Thanks man.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Get Random amounts depending on a cell value


    So in this case if still H1 & bounds match the number of rows the result may be found with the first attempt …

  10. #10
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    Marc L. Understood clearly. Checked with different number of rows and different expected result amounts. It is perfect. The beep sound is a bonus. 💜

  11. #11
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    Quote Originally Posted by Marc L View Post

    So in this case if still H1 & bounds match the number of rows the result may be found with the first attempt …
    Yup. After the match too I can change the amounts by pressing the button as many times I want.

  12. #12
    Forum Expert bebo021999's Avatar
    Join Date
    07-22-2011
    Location
    Vietnam
    MS-Off Ver
    Excel 2016
    Posts
    9,466

    Re: Get Random amounts depending on a cell value

    Alternative code:
    Hope this approach is easy to understand for you.
    This is how it works:
    1- Random every rows first (regardless the total)
    2- Check the sum (total after random), if sum>total then deduct 50 from random row
    3- Repeat
    Until the sum reaches the total.


    PHP Code: 
    Option Explicit
    Sub demo2
    ()
    Dim i&, res(), rnum&, sum As Longtota As Long
    num 
    Cells(Rows.Count"A").End(xlUp).Row 1
    tota 
    Range("H1").Value
    ReDim res
    (1 To num1 To 1)
    Randomize

    'First, genarate random value (from 450 to 9900) then save to array "res"
    For i = 1 To num
        r = (Int(Rnd * 189) + 9) * 50 ' 
    equalsrandbetween(9,189) * 50 randbetween(450,9900)
        
    res(i1) = ' generate r (=random from 450-9900)then save to result array, regardless to total in H1
    Next
    sum = WorksheetFunction.sum(res) ' 
    this random sum could be equal or more than 890000

    'Second, deduct 50 from random row, repeat until the sum reachs 890000
    Do
        r = Int(Rnd * num) + 1 ' 
    equals randbetween(1,419)
        If 
    res(r1) > 450 Then res(r1) = res(r1) - 50
        sum 
    WorksheetFunction.sum(res)
    Loop Until sum <= tota

    'afterall, paste the array back to the sheet
    Range("E2:E" & num + 1).Value = res
    End Sub 
    Quang PT

  13. #13
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,209

    Re: Get Random amounts depending on a cell value

    Whether or not is either macro works is random i.e. they can fail on an arbitrary set of values. Or loop "endlessly". Not an easy problem to solve!
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  14. #14
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Get Random amounts depending on a cell value


    Hey John !

    The reason why my demonstration is limited to nine attempts to avoid such NES (Never Ending Story)

  15. #15
    Forum Contributor
    Join Date
    11-25-2021
    Location
    INDIA
    MS-Off Ver
    2007
    Posts
    2,147

    Re: Get Random amounts depending on a cell value

    Thanks bebo021999. It is just amazing. You people are just great coders. Hopefully nothing is impossible with you guys as you can play and do anything with VBA excel if you are so so good at coding. I am so happy to be a part of this forum where I am getting full support and learning so many things, slowly but steadily.
    Thanks for the comments too bebo021999.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Return 3 different dollar amounts depending on value range of another cell
    By Nirnglaud in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-22-2019, 02:06 AM
  2. [SOLVED] Need help with an Excel formula that adds additional amounts depending on a cell value
    By afprinter in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 05-16-2019, 02:08 PM
  3. Random selections depending on cell value and %
    By soreno in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-20-2018, 11:18 AM
  4. [SOLVED] HELP - Random number genarator depending on another cell
    By stumoore in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 09-27-2017, 12:53 PM
  5. Adding random amounts in a check register
    By CJ Mac in forum Excel General
    Replies: 8
    Last Post: 08-29-2016, 10:52 PM
  6. Totaling random columns in a row of $ amounts.
    By Patches44 in forum Excel General
    Replies: 2
    Last Post: 04-02-2014, 12:05 PM
  7. Replies: 1
    Last Post: 05-27-2013, 09:52 PM

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