+ Reply to Thread
Results 1 to 20 of 20

Change randbetween amount in code

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

    Change randbetween amount in code

    Hello code experts
    I have this code which displays amount with randbetween varying from 100-9900 in the extract sheet. I need your help to edit the code and change the randbetween amount to 50250-99500. I also want the figures to be round off like 50250, 51000, 51750, etc., and not 50251,50252, and so on.
    Attached Files Attached Files
    Last edited by RAJESH SHAH; 12-19-2022 at 06:58 AM. Reason: #Solved by Andy Pope

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,996

    Re: Change randbetween amount in code

    To drop the least significant digit, use, for example,
    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


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

    Re: Change randbetween amount in code

    TMS. The code was shared by beyond excel in this group. It is perfect for getting random amounts between maybe 100 to 9990.
    The code is so complicated for me to understand. Only if there were any comments at the end of each line, it would have helped me to edit whenever necessary. I am not able to figure out which line does what. Can you please edit the code and help me get the expected result which is figures between 50250 to 99750.

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,996

    Re: Change randbetween amount in code

    I am currently using an iPad so I can’t see code in workbooks. Post the code in the thread.

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,996

    Re: Change randbetween amount in code

    Or post a link to the thread where the solution was provided, assuming the code was posted in the thread.

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

    Re: Change randbetween amount in code

    HTML Code: 

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,996

    Re: Change randbetween amount in code

    Use Code tags, not HTML tags.

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

    Re: Change randbetween amount in code

    I forgot. How do I use code tags. Done. I will try to find the link and if found I will share it here.

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

    Re: Change randbetween amount in code

    Please Login or Register  to view this content.

  10. #10
    Valued Forum Contributor
    Join Date
    08-03-2012
    Location
    Newcastle
    MS-Off Ver
    Excel 2007, 2010, 2013, 2016, Office 365
    Posts
    492

    Re: Change randbetween amount in code

    This might help..

    Please Login or Register  to view this content.
    zeddy

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

    Re: Change randbetween amount in code

    What do I define zVal, a and b as ..?

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Change randbetween amount in code

    The help on RND gives you the code to do randbetween.
    if you reduce the min and max values by a factor of 10 and then increase the random value by a factor of 10 you should get the values required.

    Please Login or Register  to view this content.

    From VBA Help

    To produce random integers in a given range, use this formula:
    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Cheers
    Andy
    www.andypope.info

  13. #13
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Change randbetween amount in code

    Is Randbetween available in xl2007?

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

    Re: Change randbetween amount in code

    I tried and edited this line
    Please Login or Register  to view this content.
    as
    Please Login or Register  to view this content.
    and it worked. zeddy, your code gave me the idea and that helped me to corect it. Thanks man. Thanks to TMS for his support.
    But there is one issue, I am getting amounts below 50,000 also. Need to edit one more line.
    Last edited by RAJESH SHAH; 12-19-2022 at 06:27 AM.

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

    Re: Change randbetween amount in code

    Andy Pope. I think it works in 2007 too and thanks for the response.

  16. #16
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,485

    Re: Change randbetween amount in code

    If it does then you can use the same approach of reducing the 2 values by 10 and increasing the result by 10.

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

    Re: Change randbetween amount in code

    No. I am unable to understand that. I tried 99,000 but it is showing once again values more than 99,000.

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

    Re: Change randbetween amount in code

    Quote Originally Posted by Andy Pope View Post
    The help on RND gives you the code to do randbetween.
    if you reduce the min and max values by a factor of 10 and then increase the random value by a factor of 10 you should get the values required.

    Please Login or Register  to view this content.

    From VBA Help

    To produce random integers in a given range, use this formula:
    Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
    Your line solved the problem. I edited like this and it gave me the expected result.
    Please Login or Register  to view this content.
    Thanks Andy Pope

  19. #19
    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
    31,225

    Re: Change randbetween amount in code

    Try

    i = Int(WorksheetFunction.RandBetween(52050, 99999) / 10) * 10
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

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

    Re: Change randbetween amount in code

    That too worked perfectly. Thank you John Topley.

+ 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. Randbetween to exclude 3 or more other randbetween with Sudoku rules
    By Mayday67 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-22-2021, 01:59 PM
  2. [SOLVED] Change code to search amount.
    By abjac in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-08-2016, 08:52 AM
  3. Replies: 10
    Last Post: 07-03-2015, 04:29 PM
  4. Sum charge amount every time the code and the order number change
    By children in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-19-2013, 08:49 AM
  5. RandBetween code in VB (Excel 2010)
    By teamyoko in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-26-2012, 01:04 AM
  6. [SOLVED] RANDBETWEEN should work only if A3, change the data
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-20-2012, 12:02 PM
  7. IF Statement in VB w/ CODE()*RANDBETWEEN()
    By nsorden in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-22-2009, 09:34 AM

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