Closed Thread
Results 1 to 10 of 10

Select Random Cells in the Selection

  1. #1
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Select Random Cells in the Selection

    I'm trying to select an x number of random selections within the selection.

    Basically, lets say A1:A100 is selected, im going to have a inputbox asking how many to select, you put 10 in the inputbox, and then it randomly chooses 10 from A1:A100 and highlights them

    This seems easy enough, i can do the input box and know the random function. just not sure how to set the beginning point and the ending point with Cell locations and then have it highlight those selections... a for loop I'm guessing?

    Any help will be greatly appreciated!
    Last edited by koticphreak; 02-15-2009 at 07:23 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Cells in the Selection

    Hi

    See if this gets you started.

    Please Login or Register  to view this content.
    rylo

  3. #3
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Select Random Cells in the Selection

    This works GREAT! except I'm having one small problem... It allows it to randomly select the same cell, so if I have 20 and tell it to randomly select 10, i may only have 8 or 9 selections because it chose the same one twice... And I don't have the slightest clue on how to go about fixing this

  4. #4
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Cells in the Selection

    Hi

    Yea, I realised that this would be an issue, but wasn't sure how you would approach it.

    Some thoughts:
    1) only allow a selection number that is small enough that there is a high chance that you won't get repeats. So if you have 20 cells, then don't allow more than say 6 to be selected.

    2) Another option is to keep cycling through the range until you have 10 distinct numbers. Maybe use a collection to store them all, and just keep looping until you reach your count

    Please Login or Register  to view this content.
    If you run the code above on a blank spreadsheet, you should get your list of unique numbers. You would have to make variables of the 10 and 20 that I have fixed above - have a look at the original code for ways to do that.

    HTH

    rylo

  5. #5
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Select Random Cells in the Selection

    Thanks, but I'm getting an object required error on line:

    Loop Until nodupes.Count = 10

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Cells in the Selection

    Hi

    Add the line

    Please Login or Register  to view this content.
    before the do statement

    rylo

  7. #7
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Select Random Cells in the Selection

    so I attempted to combine the two sets of code together and I'm sure i made a stupid mistake while doing so because I can't seem to get it to work... if someone could take a look at it and let me know what I'm doing wrong.


    Please Login or Register  to view this content.

    THANKS!

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Select Random Cells in the Selection

    Hi

    Try this.


    Please Login or Register  to view this content.

    rylo

  9. #9
    Forum Contributor
    Join Date
    09-13-2008
    Location
    Los Angeles, CA
    MS-Off Ver
    365
    Posts
    120

    Re: Select Random Cells in the Selection

    that did the trick perfectly! thanks so much!

  10. #10
    Registered User
    Join Date
    09-25-2007
    Posts
    21

    Re: Select Random Cells in the Selection

    Hi There,
    Could some one help t select data randomly as I need to select some data?
    I have a colum which contains data and I need to select same data daily and I need a macro which can ask how may need to select (using Dim arr As Variant) and once I specified number then it has to give specified number of data which it selects randomly.
    I have checked all the posting in the forum but neither one gives me clear idea please some one assist on this , attached is spread of clear understanding,
    Thanks & Regards
    Sandeep...

    INDIA

Closed 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