Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub Random20()
- 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 = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
- 'Get 20% of that number
- percRows = 225
- '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
- ThisWorkbook.Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
- Destination:=ThisWorkbook.Sheets(2).Cells(copyRow, 1)
- Next
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement