|
- Sub 随机抽取()
- arr = Sheets(1).Range("a2:p" & Sheets(1).[a65536].End(3).Row)
- all = UBound(arr) '所有记录数
- ReDim brr(1 To all) '已抽取过的记录
- Total = 20 '要抽取的总条数
- ReDim crr(1 To Total, 1 To UBound(arr, 2))
- t1 = Int(Total * 0.6): t2 = Total - t1 '单选数、多选数
- Do While n < Total
- qc: p = Int(all * Rnd + 1) '抽取
- If brr(p) = 1 Then GoTo qc '如果已抽取过,重新抽取
- If arr(p, 1) = "单选" Then t1 = t1 - 1 Else t2 = t2 - 1
- If t1 < 0 Then t1 = 0: GoTo qc '如果单选已抽满,且又抽到单选,重新抽取
- If t2 < 0 Then t2 = 0: GoTo qc
- n = n + 1 '有效记录
- brr(p) = 1
- For j = 1 To UBound(arr, 2)
- crr(n, j) = arr(p, j)
- Next
- Loop
- With Sheets(2)
- .Range("a2:p1000").ClearContents
- .[a2].Resize(n, UBound(arr, 2)) = crr
- End With
- End Sub
复制代码 |
|