Sub RandSample()
Dim arOrig, x, Key, Key1, arRes()
Dim i As Long, j As Long, iSubset As Long
Dim dicRnd As Object, dicSorted As Object, dicUnq As Object, dicRes As Object
If [H6] > [H9] Or [H6] = vbNullString Then
MsgBox "Sample size cannot exceed minimum population size!!", vbCritical
[H6].ClearContents
[H6].Activate
Exit Sub
End If
iSubset = [H6].Value
With Sheets("Input")
arOrig = .Range("A2:E" & .Cells(Rows.Count, 1).End(xlUp).Row).Value2
End With
Set dicRnd = CreateObject("Scripting.Dictionary")
Set dicUnq = CreateObject("Scripting.Dictionary")
For i = 1 To UBound(arOrig)
Do
x = Rnd
Loop Until Not dicRnd.Exists(x)
dicUnq(arOrig(i, 1)) = dicUnq.Count
dicRnd(x) = Join(Application.Index(arOrig, i, 0), "~")
Next
Set dicSorted = dictKeySortAscending(dicRnd)
For Each Key In dicRnd.Keys
dicSorted(Key) = dicRnd(Key)
Next
Set dicRes = CreateObject("Scripting.Dictionary")
For Each Key In dicUnq.Keys
i = 0
Do While i < iSubset
For Each Key1 In dicSorted.Keys
If CStr(Key) = Split(dicSorted(Key1), "~")(0) Then
dicRes(dicUnq(Key) & "," & i) = dicSorted(Key1)
dicSorted.Remove Key1
Exit For
End If
Next
i = i + 1
Loop
Next
ReDim arRes(dicRes.Count, 5)
i = 0
For Each Key In dicRes.Keys
x = Split(dicRes(Key), "~")
For j = 0 To 4
arRes(i, j) = x(j)
Next
i = i + 1
Next
Sheet3.Range("A2").Resize(dicRes.Count, 5) = arRes
MsgBox "Record Count:= " & dicRes.Count & vbNewLine & "Unique Names:= " & dicUnq.Count & vbNewLine & "Record/Name:= " & iSubset
End Sub