|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
本帖最后由 micch 于 2020-3-9 14:41 编辑
水平不够,瞎写一个吧。列出的结果到是不多。因为组合的时候,按每个计算式只组合一次,不会重复出现进行的组合。
比如A+B,x3+x6=22和x1+x4=21.4,误差不到0.9是可以组合的,但是因为21.4和x3+x8=21已经组合过了,所以结果里就没有x3+x6这个计算式了。
- Sub test()
- Dim arr, ar, brr(1 To 33333, 1 To 10), d, x%, i%, j%, k%, r&, n&, m%, s
- w = [b2].Value
- arr = Range("A5", [b5].End(4))
- x = UBound(arr)
- Set d = CreateObject("scripting.dictionary")
-
- For i = 1 To 9 Step 2
- Set d(i) = CreateObject("scripting.dictionary")
- Next
- For i = 1 To x
- For k = i To x '组合1
- d(1)(arr(i, 1) + "+" + arr(k, 1)) = arr(i, 2) + arr(k, 2)
- For j = k To x '组合4
- d(7)(arr(i, 1) + "+" + arr(k, 1) + "+" + arr(j, 1)) = arr(i, 2) + arr(k, 2) + arr(j, 2)
- Next j, k, i
- For Each s In d(1).keys '组合2
- For i = 1 To x
- If InStr(s, arr(i, 1)) = 0 Then
- If d(1)(s) - arr(i, 2) >= 0 Then d(3)(s + "-" + arr(i, 1)) = d(1)(s) - arr(i, 2)
- For k = i To x '组合3
- If InStr(s, arr(k, 1)) = 0 Then
- If d(1)(s) - arr(i, 2) - arr(k, 2) >= 0 Then d(5)(s + "-" + arr(i, 1) + "-" + arr(k, 1)) = d(1)(s) - arr(i, 2) - arr(k, 2)
- End If
- Next
- End If
- Next i, s
- For Each s In d(7).keys '组合5
- For i = 1 To x
- If InStr(s, arr(i, 1)) = 0 Then
- For k = i To x
- If InStr(s, arr(k, 1)) = 0 Then
- If d(7)(s) - arr(i, 2) - arr(k, 2) >= 0 Then d(9)(s + "-" + arr(i, 1) + "-" + arr(k, 1)) = d(7)(s) - arr(i, 2) - arr(k, 2)
- End If
- Next
- End If
- Next i, s
-
- For i = 1 To 9 Step 2
- ar = Application.Transpose(Array(d(i).keys, d(i).items))
- r = 0
- Call px(ar, brr, r, 0, w, i)
- Next
- [c5:l9999].ClearContents
- [c5].Resize(r, 10) = brr
- End Sub
- Sub px(ar, brr, r, n, w, m)
- For i = 1 To UBound(ar)
- For k = i + 1 To UBound(ar)
- If ar(i, 2) > ar(k, 2) Then
- t = ar(i, 1): ar(i, 1) = ar(k, 1): ar(k, 1) = t
- y = ar(i, 2): ar(i, 2) = ar(k, 2): ar(k, 2) = y
- End If
- Next k, i
- For i = 1 To UBound(ar) - 1
- If ar(i + 1, 2) - ar(i, 2) <= w Then
- a = ar(i, 2): r = r + 1: n = n + 1
- brr(r, m) = "第 " & n & " 组"
- r = r + 1
- brr(r, m) = ar(i, 1): brr(r, 1 + m) = ar(i, 2)
- r = r + 1
- brr(r, m) = ar(i + 1, 1): brr(r, m + 1) = ar(i + 1, 2)
- Do
- i = i + 1
- If ar(i + 1, 2) - a <= w Then
- r = r + 1
- brr(r, m) = ar(i + 1, 1): brr(r, m + 1) = ar(i + 1, 2)
- Else
- Exit Do
- End If
- Loop
- End If
- Next
- End Sub
复制代码
|
评分
-
2
查看全部评分
-
|