Advertisement
Murdlih

kseguf79

Jun 9th, 2022
1,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.  
  3. Sub kseguf79()
  4. Dim srcWS As Worksheet      'Worksheet I want to grab data from
  5. Dim destWS As Worksheet     'Worksheet where I want to drop data
  6. Dim i As Long               'for iterating through things
  7. Dim usedRowsDic As Object   'keeping track of rows I have already selected
  8. Dim outArray() As Variant   'the array data structure I want to store my records in for eventual output
  9. Dim lastrow As Long         'the last row of the source worksheet
  10. Dim bigArray As Variant     'read all the data into an array for fasterer processing
  11. Dim desiredRecs As Long     'number of records to select
  12. Dim recsGathered As Long    'running count of records I have gathered so far
  13. Dim rec As Long             'record I want to add/check right noe
  14.  
  15.     'set workbooks, dictionary
  16.    Set srcWS = ThisWorkbook.Worksheets("Source Data")
  17.     Set destWS = ThisWorkbook.Worksheets("Destination")
  18.     Set usedRowsDic = CreateObject("scripting.dictionary")
  19.    
  20.     'number of records to select
  21.    desiredRecs = 300
  22.    
  23.     'find the last row of our data
  24.    lastrow = srcWS.Range("A" & Rows.Count).End(xlUp).Row
  25.    
  26.     'our full dataset - adjust columns as needed
  27.    bigArray = srcWS.Range("A2:H" & lastrow)
  28.    
  29.     'resize our outarray to accomodate the number of records desired
  30.    ReDim outArray(LBound(bigArray, 1) To desiredRecs, LBound(bigArray, 2) To UBound(bigArray, 2))
  31.    
  32. 'select random records
  33.    Do Until recsGathered = desiredRecs
  34.         Randomize
  35.         'select one of our records at random, if we have 20 rows, select a random number between 1, 20
  36.        rec = WorksheetFunction.RandBetween(LBound(bigArray, 1), UBound(bigArray, 1))
  37.         'check if this record number is already in our dictionary, if not, add it to our dictionary
  38.        If Not usedRowsDic.exists(rec) Then
  39.             'increment our "Gathered records" counter
  40.            recsGathered = recsGathered + 1
  41.             'populate our "outarray" with data from our "BigArray" - may be a better way to do this
  42.            For i = LBound(outArray, 2) To UBound(outArray, 2)
  43.                 outArray(recsGathered, i) = bigArray(rec, i)
  44.             Next i
  45.             'add this record number to our dictionary so we don't select it again
  46.            usedRowsDic.Add rec, True
  47.         End If
  48.     Loop
  49. 'output
  50.    'drop out our "outarray" when we are done
  51.    destWS.Range("A2").Resize(desiredRecs, UBound(outArray, 2)).Value = outArray
  52. '    MsgBox (usedRowsDic.Count)
  53.    Erase outArray
  54.     Erase bigArray
  55.     Set usedRowsDic = Nothing
  56.     Set srcWS = Nothing
  57.     Set destWS = Nothing
  58.  
  59. End Sub
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement