Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. Sub Random20()
  2. Randomize 'Initialize Random number seed
  3. Dim MyRows() As Integer ' Declare dynamic array.
  4. Dim numRows, percRows, nxtRow, nxtRnd, chkRnd, copyRow As Integer
  5. 'Determine Number of Rows in Sheet1 Column A
  6. numRows = ThisWorkbook.Sheets(1).Range("A" & Rows.Count).End(xlUp).Row
  7. 'Get 20% of that number
  8. percRows = 225
  9. 'Allocate elements in Array
  10. ReDim MyRows(percRows)
  11. 'Create Random numbers and fill array
  12. For nxtRow = 1 To percRows
  13. getNew:
  14. 'Generate Random number
  15. nxtRnd = Int((numRows) * Rnd + 1)
  16. 'Loop through array, checking for Duplicates
  17. For chkRnd = 1 To nxtRow
  18. 'Get new number if Duplicate is found
  19. If MyRows(chkRnd) = nxtRnd Then GoTo getNew
  20. Next
  21. 'Add element if Random number is unique
  22. MyRows(nxtRow) = nxtRnd
  23. Next
  24. 'Loop through Array, copying rows to Sheet2
  25. For copyRow = 1 To percRows
  26. ThisWorkbook.Sheets(1).Rows(MyRows(copyRow)).EntireRow.Copy _
  27. Destination:=ThisWorkbook.Sheets(2).Cells(copyRow, 1)
  28. Next
  29. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement