+ Reply to Thread
Results 1 to 3 of 3

VBA for random generator

  1. #1
    Registered User
    Join Date
    04-04-2012
    Location
    Kampala
    MS-Off Ver
    Excel 2011
    Posts
    4

    VBA for random generator

    I have a list of 500 names in CSV, i want to have a visual basic graphic code or a macro that can help me select 5 winners of these

  2. #2
    Forum Expert dilipandey's Avatar
    Join Date
    12-05-2011
    Location
    Dubai, UAE
    MS-Off Ver
    1997 - 2016
    Posts
    8,191

    Re: VBA for random generator

    Hi arn2025,

    Enter the below formula in the next column and drag it down 5 cells..

    =OFFSET($A$1,RANDBETWEEN(0,499),0)

    Regards,
    DILIPandey

    <click on below 'star' if this helps>
    DILIPandey, Excel rMVP
    +919810929744 (India), +971528225509 (Dubai), [email protected]

  3. #3
    Registered User
    Join Date
    04-03-2012
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: VBA for random generator

    You can make a simple random number generator as a function that you can reuse as needed. Then you can make a loop that uses the number generator as many times as you like. If you're only getting 5 out of 500, you shouldn't have too much trouble with getting a unique number with a brute force method. Then you use the random number as an index for your names.

    This is a pretty typical RNG function:
    Please Login or Register  to view this content.
    lowerbound is the lowest possible number and upperbound is the highest, so you would call RandNum(1,500) to get a number between 1 and 500. Here's a macro that returns five unique numbers, using the above function:

    Please Login or Register  to view this content.
    I'm sure there is a more elegant approach, but that will get the job done. Then you just use an offset for the names according the randomly selected indices.

+ 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