|
Sub test()
Dim ar, br, vResult, i&, xNum&, dic As Object, vKey, isFlag As Boolean
Application.ScreenUpdating = False
ar = [A1].CurrentRegion.Value
ReDim vResult(1 To UBound(ar), 0): vResult(1, 0) = "RESULT"
ReDim br(1 To UBound(ar))
Set dic = CreateObject("Scripting.Dictionary")
For i = 2 To UBound(ar)
If Len(ar(i, 1)) Then
br(i) = Split(ar(i, 1), ",")
xNum = Int((UBound(br(i)) + 1) * Rnd)
vResult(i, 0) = br(i)(xNum)
dic(vResult(i, 0)) = dic(vResult(i, 0)) + 1
End If
Next i
isFlag = True
For Each vKey In dic.keys
If dic(vKey) > 50 Then isFlag = False: Exit For
Next
Do Until isFlag = True
For i = 2 To UBound(vResult)
If Len(vResult(i, 0)) Then
If dic(vResult(i, 0)) > 50 Then
xNum = Int((UBound(br(i)) + 1) * Rnd())
dic(vResult(i, 0)) = dic(vResult(i, 0)) - 1
dic(br(i)(xNum)) = dic(br(i)(xNum)) + 1
vResult(i, 0) = br(i)(xNum)
isFlag = True
For Each vKey In dic.keys
If dic(vKey) > 50 Then isFlag = False: Exit For
Next
If isFlag = True Then Exit Do
End If
End If
Next i
Loop
[T1].Resize(UBound(vResult)) = vResult
[W1].Resize(dic.Count, 2) = Application.Transpose(Array(dic.keys, dic.items))
Set dic = Nothing
Application.ScreenUpdating = True
End Sub
|
评分
-
1
查看全部评分
-
|