test.rar
(18.38 KB, 下载次数: 6)
'数量随机数间隔分布不太好,不过效率还算可以的
'品5"、"品8"放最后了,为了计算方便
Option Explicit
Sub test()
Dim arr, i, j, cnt, total As Single, pri_ave As Single, n, pri_sum, m, t As Single, num
arr = [a4:c20]
ReDim brr(1 To UBound(arr, 1), 1 To UBound(arr, 2))
For i = 1 To UBound(arr, 1) - 1
If arr(i, 1) <> "品5" And arr(i, 1) <> "品8" Then
n = n + 1
For j = 1 To UBound(arr, 2): brr(n, j) = arr(i, j): Next
pri_sum = pri_sum + arr(i, 2)
Else
cnt = cnt + arr(i, 3)
total = total + arr(i, 2) * arr(i, 3)
End If
Next
cnt = arr(UBound(arr, 1), 3) - cnt
total = arr(UBound(arr, 1), 2) * arr(UBound(arr, 1), 3) - total
pri_ave = Round(total / cnt, 0)
Randomize
Do
t = 0: num = 0
For i = 1 To n
m = Round(total / n / arr(i, 2) * (0.8 + Rnd / 5) * arr(i, 2) / pri_ave, 1)
brr(i, 3) = m: num = num + m
t = t + brr(i, 2) * m
Next
num = Round(cnt - num, 1)
t = Round(total - t, 1)
If t > 0 And num > 0 Then
For i = 1 To n
If num * brr(i, 2) = t Then brr(i, 3) = brr(i, 3) + num: Exit Do
Next
End If
DoEvents
Loop
For i = 1 To UBound(arr, 1) - 1
If arr(i, 1) = "品5" Or arr(i, 1) = "品8" Then
n = n + 1
brr(n, 1) = arr(i, 1): brr(n, 2) = arr(i, 2): brr(n, 3) = arr(i, 3)
End If
Next
[e4].Resize(n, UBound(arr, 2)) = brr
End Sub
|