+ Reply to Thread
Results 1 to 11 of 11

Randomly select from long list

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Randomly select from long list

    Hi,

    I have a long list of rows, and what I would like to is to randomly select 60 rows from the long list with a click of a button.

    I'm not sure what the best approach would be.

    Can anybody shed some light on this please?

    Please see the attached.

    Thank you
    Attached Files Attached Files

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Randomly select from long list

    Lifeseeker,

    Attached is a modified version of your example workbook. I copied the list of IDs to Sheet2, column A. In Sheet2, column B I then entered the formula =RAND(). Then I set Sheet2 to hidden.

    Here's the macro assigned to the button:
    Private Sub CommandButton1_Click()
        
        Randomize
        Sheets("Sheet2").Calculate
        Intersect(Sheets("Sheet2").UsedRange, Sheets("Sheet2").Columns("A:B")).Sort Sheets("Sheet2").Range("B1"), xlAscending, , , , , , xlNo
        ActiveSheet.Range("D4:D63").Value = Sheets("Sheet2").Range("A1:A60").Value
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Randomly select from long list

    Oh wow. Thank you

    Could you explain this part please?

    Randomize
        Sheets("Sheet2").Calculate
        Intersect(Sheets("Sheet2").UsedRange, Sheets("Sheet2").Columns("A:B")).Sort Sheets("Sheet2").Range("B1"), xlAscending, , , , , , xlNo
    Does "randomize" do the randomizing by default? The Intersect() is looking for the intersection between the used range and A and B in Sheet2?

  4. #4
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Randomly select from long list

    Randomize shuffles Excel's randomization functions to start at a new location. After that, the .Calculate is what's causing the =RAND() functions to pull new random numbers, based on the current location of Excel's randomization table. In Excel's help file [F1], the Randomize Statement is defined as: "Intitializes the random-number generator."

    the Intersect() method catches all items in columns A and B that are in sheet 2's used range. Note that this isn't strictly necessary because the only items in sheet2's used range are columns A and B. If you ever updated sheet2 with extra info in the other columns though, it wouldn't upset the macro because its using intersect.

  5. #5
    Valued Forum Contributor
    Join Date
    10-21-2011
    Location
    Canada
    MS-Off Ver
    Excel 2010
    Posts
    513

    Re: Randomly select from long list

    THank you.

    I would like to put the result to a new workbook or a new sheet within the active workbook. How would I do so?

  6. #6
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: Randomly select from long list

    To a new sheet in the same workbook:
    Private Sub CommandButton1_Click()
        
        Randomize
        Sheets("Sheet2").Calculate
        Intersect(Sheets("Sheet2").UsedRange, Sheets("Sheet2").Columns("A:B")).Sort Sheets("Sheet2").Range("B1"), xlAscending, , , , , , xlNo
        Sheets.Add(After:=Sheets(Sheets.Count)).Range("A1:A60").Value = Sheets("Sheet2").Range("A1:A60").Value
        
    End Sub


    To a new workbook:
    Private Sub CommandButton1_Click()
        
        Randomize
        Sheets("Sheet2").Calculate
        Intersect(Sheets("Sheet2").UsedRange, Sheets("Sheet2").Columns("A:B")).Sort Sheets("Sheet2").Range("B1"), xlAscending, , , , , , xlNo
        With Sheets.Add(After:=Sheets(Sheets.Count))
            .Range("A1:A60").Value = Sheets("Sheet2").Range("A1:A60").Value
            .Move
        End With
        
    End Sub

+ 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