|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
本帖最后由 lkqsmxw 于 2020-3-13 18:18 编辑
Sub suiji()
Dim STR1, M, X, N, Rng, rn, i, k
'自己测试后,再修改一下吧
For i = 1 To 300 '抽取次数
STR1 = ""
M = 3
k = 0
[a1:j10].Interior.ColorIndex = xlNone
Set Rng = [a1:j10]
CF:
For N = M To 0 Step -1
X = WorksheetFunction.RandBetween(1, Rng.Cells.Count)
Set rn = Rng.Cells(X).Offset(0, 0).Resize(1, 1)
If rn.Interior.ColorIndex <> 4 Then
M = M - 1
If M >= 0 Then
k = k + 1 '抽取单元格个数
rn.Interior.ColorIndex = 4
If STR1 = "" Then STR1 = rn Else STR1 = STR1 & "," & rn
End If
End If
Next N
If M > 0 Then GoTo CF
Cells(Rows.Count, "m").End(3).Offset(1) = STR1
Cells(Rows.Count, "l").End(3).Offset(1) = k
Next i
End Sub
|
|