Results 1 to 3 of 3

Random sampling based on specific criteria from another column

Threaded View

  1. #1
    Registered User
    Join Date
    08-19-2010
    Location
    Arizona, USA
    MS-Off Ver
    Excel Mac 2011
    Posts
    59

    Random sampling based on specific criteria from another column

    Hello again!

    I have a workbook that contains data only in Sheet 1
    Row 1 contains Headers - The number of rows can vary from 2 - 20,000+
    Columns = A thru N
    Col A = Unique ID (Number)
    Col N = Names (Contains Duplicates)
    Cols B thru L contains mixed data

    I am using the following macro to copy a RANDOM 10% of every row in Sheet 1 into Sheet 2 which works great. The problem I have is I need to ensure that at least ONE of every Name listed in Col N is included in the 10% copied to Sheet 2.

    ' Option Explicit
        Sub Random10()
        
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
        
        Randomize 'Initialize Random number seed
        Dim MyRows() As Integer ' Declare dynamic array.
        Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
        'Determine Number of Rows in Sheet1 Column A
        numRows = Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
        'Get 10% of that number
        percRows = numRows * 0.1
        'Allocate elements in Array
        ReDim MyRows(percRows)
        'Create Random numbers and fill array
        For nxtRow = 1 To percRows
    getNew:
        'Generate Random number
        nxtRnd = Int((numRows) * Rnd + 1)
        'Loop through array, checking for Duplicates
        For chkRnd = 1 To nxtRow
        'Get new number if Duplicate is found
        If MyRows(chkRnd) = nxtRnd Then GoTo getNew
        Next
        'Add element if Random number is unique
        MyRows(nxtRow) = nxtRnd
        Next
        'Loop through Array, copying rows to Sheet2
        For copyRow = 1 To percRows
        Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
        Destination:=Sheets(2).Cells(copyRow, 1)
        On Error Resume Next
           Application.ScreenUpdating = True
       Application.Calculation = xlCalculationAutomatic
        
        End Sub
        Next
    Any help would be much appreciated!
    Last edited by symaxf; 07-06-2016 at 06:42 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Creating controlled random numbers across multiple columns
    By terryhenderson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-27-2016, 12:20 PM
  2. [SOLVED] Random List Generator How to copy Columns A and B to Sheet 2
    By guitarsweety in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-08-2014, 09:01 PM
  3. Random number generation across multiple ros/columns without repeats
    By Zodeeak in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 08-27-2014, 03:15 PM
  4. [SOLVED] select random cells from multiple sheets and copy
    By cfinch100 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-10-2014, 06:24 PM
  5. Replies: 9
    Last Post: 12-15-2013, 10:05 PM
  6. Replies: 4
    Last Post: 12-22-2012, 01:41 PM
  7. copy random columns
    By bhatrakesh@gmail.com in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-02-2011, 07:38 AM

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