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!
Bookmarks