willow1985
Well-known Member
- Joined
- Jul 24, 2019
- Messages
- 888
- Office Version
- 365
- Platform
- Windows
Hello, I have this Random Sample code that I obtained from a very kind gentleman from this site, however I wish to make a modification to it and am unsure how to do so (many attempts have failed with errors).
I need 2 variations of the code.
The current code pulls a random sample from column 1 in a table based on the number provided in C1.
This is all fine however it also selects based on the condition of any cell that has a value "Yes" in column 14.
I need the part of the code that uses the condition of having "yes" in column 14 removed (not needed)
AND
another code that uses the condition "yes" based on the column header name "Mark" NOT position 14.
Would anyone be able to provide the 2 codes with these modifications?
Thank you very much!
ORIGINAL CODE:
I need 2 variations of the code.
The current code pulls a random sample from column 1 in a table based on the number provided in C1.
This is all fine however it also selects based on the condition of any cell that has a value "Yes" in column 14.
I need the part of the code that uses the condition of having "yes" in column 14 removed (not needed)
AND
another code that uses the condition "yes" based on the column header name "Mark" NOT position 14.
Would anyone be able to provide the 2 codes with these modifications?
Thank you very much!
ORIGINAL CODE:
VBA Code:
Sheets("Random Sample").Select
Dim R As Long, Cnt As Long, RandomIndex As Long, HowMany As Long, Arr As Variant, Tmp As Variant
HowMany = Sheets("Random Sample").Range("C1").Value
Randomize
Arr = Sheets("Table").ListObjects("Data").DataBodyRange.Value
With CreateObject("Scripting.Dictionary")
For R = 1 To UBound(Arr)
If Arr(R, 14) = "Yes" Then .Item(CStr(Arr(R, 1))) = 1
Next
Arr = .Keys
End With
For Cnt = UBound(Arr) To LBound(Arr) Step -1
RandomIndex = Int((Cnt - LBound(Arr) + 1) * Rnd + LBound(Arr))
Tmp = Arr(RandomIndex)
Arr(RandomIndex) = Arr(Cnt)
Arr(Cnt) = Tmp
Next
Sheets("Random Sample").Range("B3").Resize(HowMany) = Application.Transpose(Arr)