+ Reply to Thread
Results 1 to 7 of 7

Using VBA to generate random numbers

  1. #1
    Registered User
    Join Date
    09-01-2008
    Location
    Halifax, UK
    Posts
    5

    Unhappy Using VBA to generate random numbers

    Dear Forum,

    I am using the VBA code below (bottom) to return random numbers for a selected area of cells where the range of random numbers is between 1 and the
    total number of cells. Unfortunately sometimes the random numbers appear more than once and I would like to amend this to ensure duplicates are not included, any help would be greatly appreciated, thanks, Matt

    Ps I have already tried substituting with VBUniqRandInt() but it returns #NAME? in every selected cell??, i.e;

    Please Login or Register  to view this content.
    Last edited by shg; 09-01-2008 at 11:51 AM. Reason: add code tags

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I have edited your post to add Code Tags.

    Please read the Forum Rules.

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    You can enter the function below as an array formula in the range where you wish non-repeated numbers to appear. For example, in A1:A10, enter =RandSeq()

    Array formulas MUST be confirmed with Ctrl+Shift+Enter, not Enter. You'll know you did it correctly if curly braces appear around the formula in the Formula Bar; you cannot type in the braces directly.

    Please Login or Register  to view this content.
    Last edited by shg; 09-01-2008 at 12:09 PM.

  4. #4
    Registered User
    Join Date
    09-01-2008
    Location
    Halifax, UK
    Posts
    5
    Hi Shg,

    Advance apologies but I am a complete novice with VBA and as such I'm not sure I entirely understand your approach. The vba code I used originally (below) relied on a user selecting a range of cells and then when the code was run it generated and populated the selected cells with random (albeit not non-repeating) numbers. Should I add your code as a seperate function and then run as a sub proc replacing the "=int(Rand() ..with "=int(RandSeq()?

    Ta, Matt

    original code thats giving duplicated values -

    Sub MakeRandomTable4()

    For Each r In Selection
    r.Formula = "=int(Rand() *" & Selection.Count & "+1)"
    Next
    End Sub

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Matt!

    Please read the Forum Rules and then edit your post to wrap your code with Code Tags.

    You select the range of cells in which you want the numbers to appear, and then array-enter the formula. Read the first line in my last post. But first, WRAP YOUR CODE WITH CODE TAGS.

  6. #6
    Registered User
    Join Date
    09-01-2008
    Location
    Halifax, UK
    Posts
    5

    Smile Using VBA to generate random numbers

    Hi Shg,

    Sorry for not using the proper conventions when editing code, I'm very new to these forums but I will ensure I use code tags within any future posts. Also thankyou for giving me a solution that worked as intended - I persevered and managed to understand how to apply it as you described, much appreciated.
    Best rgds
    Matt

  7. #7
    Forum Expert
    Join Date
    08-27-2008
    Location
    England
    MS-Off Ver
    2010
    Posts
    2,561

    Wink

    Quote Originally Posted by shg View Post
    Matt!

    Please read the Forum Rules and then edit your post to wrap your code with Code Tags.

    You select the range of cells in which you want the numbers to appear, and then array-enter the formula. Read the first line in my last post. But first, WRAP YOUR CODE WITH CODE TAGS.
    I'm impressed, that exclamation mark is far more discreet than what I might have written...

+ 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. Random number generator and matching all numbers
    By Zyphon in forum Excel General
    Replies: 2
    Last Post: 03-14-2008, 04:40 AM
  2. Random Numbers
    By zinzah in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2008, 01:12 PM
  3. Generate Random Numbers
    By Rgaherty in forum Excel General
    Replies: 5
    Last Post: 10-30-2007, 04:08 PM
  4. generate random numbers from a frequency list
    By adeina in forum Excel General
    Replies: 11
    Last Post: 06-21-2007, 11:34 AM
  5. Generate random numbers with no repeats
    By Paul Sheppard in forum Excel General
    Replies: 4
    Last Post: 04-06-2007, 12:37 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