+ Reply to Thread
Results 1 to 7 of 7

Randomly select 25% of list and copy to new tab

Hybrid View

  1. #1
    Registered User
    Join Date
    05-19-2014
    Posts
    6

    Randomly select 25% of list and copy to new tab

    Ok, so I cannot figure out how to get this to output only 25% of my original list. This list is dynamic and will change. I just want to be clear I want to randomly select 25% of this list and copy it to another tab. Any help would be appreciated.
    Sub RandomPull()
    Dim q2 As Integer
    Dim ws As Worksheet
    Dim Keyl As Range
    Dim Rowi As Integer
    Dim NumOfK As Integer
    Set ws = Worksheets("Key List")
    Set Keyl = ws.Range("Table5[Key]")
    Rowi = Keyl.Rows.Count
    q2 = 25
    NumOfK = Round(q2 / 100 * Rowi)
        Dim myNames As Variant
        Dim Temp As String
        Dim i As Integer
        Dim rIndex As Integer
        Dim outRRay(1 To 25, 1 To 4) As String
        myNames = Application.Transpose(Range("Table5[Key]").Value)
        randomize
        For i = 1 To Rowi
            rIndex = Int(1 + (Rnd() * NumOfK))
            Temp = myNames(i)
            myNames(i) = myNames(rIndex)
            myNames(rIndex) = Temp
        Next i
        For rIndex = 1 To 25
            For i = 0 To 3
                outRRay(rIndex, i + 1) = myNames((25 * i) + rIndex)
            Next i
        Next rIndex
        Worksheets("Random List").Range("a:a").Value = outRRay
    End Sub

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Randomly select 25% of list and copy to new tab

    Hi fatboyzfishing,

    Welcome to the forum!!

    Let me know how this goes:

    Option Explicit
    Sub Macro1()
    
        Dim ws As Worksheet
        Dim lngEndRow As Long
        Dim lngLoopCount As Long
        Dim objRowNumbers As Object
        Dim lngMyRow As Long
        
        Application.ScreenUpdating = False
        
        Set ws = Worksheets("Key List")
        lngEndRow = ws.Cells(Rows.Count, "A").End(xlUp).Row 'Sets the 'lngEndRow' variable from Col. A of the ws tab. Change to suit.
        Set objRowNumbers = CreateObject("Scripting.Dictionary")
        
        Randomize 'Without this the output will be the same
        
        Do Until lngLoopCount = Int(lngEndRow / 4) 'Rounds down to the nearest integer ie for 26 items it will run 6 times.
        
            lngMyRow = Rnd * (lngEndRow - 1) + 1
            If lngMyRow > 0 Then
                If objRowNumbers.Exists(CStr(lngMyRow)) = False Then
                    If InStr(ws.Name, " ") > 0 Then 'Tabs with a space(s) need to be incased within single quotes
                        With Sheets("Random List").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                            .Formula = "='" & CStr(ws.Name) & "'!A" & lngMyRow
                            .Value = .Value 'Converts the formula from the line above to a value. Comment out to leave original formula as is.
                        End With
                    Else
                        With Sheets("Random List").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
                            .Formula = "=" & CStr(ws.Name) & "!A" & lngMyRow
                            .Value = .Value 'Converts the formula from the line above to a value. Comment out to leave original formula as is.
                        End With
                    End If
                    objRowNumbers.Add CStr(lngMyRow), lngLoopCount
                    lngLoopCount = lngLoopCount + 1
                End If
            End If
            
        Loop
        
        Set ws = Nothing
        Set objRowNumbers = Nothing
        
        Application.ScreenUpdating = True
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Randomly select 25% of list and copy to new tab

    Try running this:

    Sub RandomPull2()
        Dim r As Range
        Dim rs As Long, r2 As Double, c As Long, max As Long
        
        Set r = Worksheets("Key List").Range("Table5[Key]")
        max = r.rows.Count
        rs = CLng(max / 100 * 25) 'percentage - adjust accordingly
        
        c = 0
        Randomize
        With CreateObject("scripting.dictionary")
            Do While c < rs
                r2 = Rnd
                r2 = (r2 * 1000000) - ((max - 1) * Int((r2 * 1000000) / (max - 1))) + 1
                Do While .Exists(r(r2).Value)
                    r2 = Rnd
                    r2 = (r2 * 1000000) - ((max - 1) * Int((r2 * 1000000) / (max - 1))) + 1
                Loop
                .Add r(r2).Value, 1
                c = c + 1
            Loop
            Worksheets("Random List").Range("A1").Resize(.Count) = Application.Transpose(.keys)
        End With
        Set r = Nothing
    End Sub
    多么想要告诉你 我好喜欢你

  4. #4
    Registered User
    Join Date
    05-19-2014
    Posts
    6

    Re: Randomly select 25% of list and copy to new tab

    Thank Trebor and milz for the help! Both sets of code worked like a charm.

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: Randomly select 25% of list and copy to new tab

    You're welcome

  6. #6
    Registered User
    Join Date
    02-14-2023
    Location
    Ohio
    MS-Off Ver
    Excel
    Posts
    1

    Re: Randomly select 25% of list and copy to new tab

    Hello!

    I have a similar issue. I have a dynamic list of numbers (that changes each week). I would like to divide this list into 6 lists (randomly selected) with each list copied onto a separate tab. We can assume my range would start in A2 for the original list.

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Randomly select 25% of list and copy to new tab


    Hello,

    just fit the post #2 or 3 code to your need …

    If you need some help, rather than your guessing challenge hijacking and digging out an eight years old thread
    create your own with your best explanation without forgetting to attach your obvious static workbook at least …

+ 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. [SOLVED] Randomly select one of two cells to populate from a list
    By 90Shilling in forum Excel General
    Replies: 1
    Last Post: 10-17-2013, 12:04 PM
  2. [SOLVED] if restriction is not met, select another word randomly from the list
    By bloem in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 07-11-2013, 10:52 AM
  3. Randomly select from long list
    By Lifeseeker in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-17-2011, 08:08 PM
  4. randomly select 30 products and copy the result
    By WasWodge in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-30-2011, 04:35 PM
  5. How to randomly select from a list with condition
    By kathyxyz in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-27-2005, 11:19 AM

Tags for this Thread

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