本帖最后由 qinhuan66 于 2014-10-27 21:06 编辑
改了一下还是不行呢。
- Dim sj1, sj(), jg(), d&, l&, h&, h2&, m&, n&, n2&, k&, cnt&
- Sub kagawa_22()
- tms = Timer
- d = [H3]: l = [H6]: If l = 0 Then l = 65535
- h = [H1] * 10 ^ d: h2 = [h2] * 10 ^ d: If h2 > h Then h2 = h2 - h
- m = [C1].End(4).Row - 1
- [a2:a65536] = "": [a2] = 1: [a2].Resize(m).DataSeries Rowcol:=xlColumns
- n = [H4]: n2 = [H5]: If n2 = 0 Then If n = 0 Then n2 = m Else n2 = n
- [a2].Resize(m, 5).Sort [D2], 1, , , , , , 2
- sj0 = [a2].Resize(m, 4)
- sj1 = [a2].Resize(m, 7)
- For i = 1 To m
- sj1(i, 1) = "+A" & sj1(i, 1) 'Row
- sj1(i, 3) = "+" & sj1(i, 3) 'Code
- sj1(i, 4) = "+" & sj1(i, 4) 'Val
- sj1(i, 5) = "" 'Chk
- sj1(i, 6) = i 'i
- sj1(i, 7) = sj1(i, 4) * 10 ^ d 'Val2
- Next
- ReDim jg(l, 5): jg(0, 0) = "序号": jg(0, 1) = "组合号": jg(0, 2) = "目标金额": jg(0, 3) = "金额组合": jg(0, 4) = "所在单元格": jg(0, 5) = "单位名称组合"
- k = 0: cnt = 0
- For j = 1 To l
- l = 0
- ReDim sj(m, 1 To 8): m = 0
- For i = 1 To UBound(sj1)
- If sj1(i, 4) = "" Then
- m = m + 1
- sj(m, 1) = sj1(i, 1) 'Row
- sj(m, 3) = sj1(i, 3) 'Code
- sj(m, 4) = sj1(i, 4) 'Val
- sj(m, 5) = sj1(i, 5) 'Chk
- sj(m, 6) = sj1(i, 6) 'i
- sj(m, 7) = sj1(i, 7) 'Val2
- sj(m, 8) = sj(m - 1, 8) + sj(m, 7) 'Σ
- End If
- Next
- Call dgH22(h, "", "", "", "", m + 1, 1)
- ' [a2].Resize(m, 4) = sj1
- If k < j Then Exit For
- Next
- If k And k < 65535 Then [K1].CurrentRegion = "": [K1].Resize(k + 1, 6) = jg
- [H7] = cnt
- [H8] = Format(Timer - tms, "0.000s")
- MsgBox "Result: " & k & "/ Calc " & cnt & " Time: " & Format(Timer - tms, "0.000s")
- m = UBound(sj0)
- [a2].Resize(m, 5) = sj1
- [a2].Resize(m, 4) = sj0
- [a2].Resize(m, 5).Sort [D2], 1, [a2], , 1, , , 2
- End Sub
- Sub dgH22(r&, ri$, ra$, rc$, rv$, i&, t&)
- Dim j&, t1&, r2&
- If l Then Exit Sub
- cnt = cnt + 1
- If t >= n And t <= n2 Then
- r2 = r + h2
- For j = 1 To i - 1
- t1 = sj(j, 7)
- If r <= t1 And t1 <= r2 Then
- k = k + 1
- jg(k, 0) = k
- jg(k, 1) = t
- jg(k, 2) = (h - r + sj(j, 6)) * 10 ^ -d
- jg(k, 3) = rv & sj(j, 3)
- jg(k, 4) = ra & sj(j, 1)
- jg(k, 5) = rc & sj(j, 2)
- x = Split(ri & "," & j, ",")
- For l = 1 To UBound(x)
- sj1(sj(x(l), 5), 4) = k
- Next
- Exit Sub
- ElseIf t1 > r2 Then
- Exit For
- End If
- Next
- End If
- If t = n2 Then Exit Sub
- For j = i - 1 To 2 Step -1
- If sj(j, 6) < r + h2 Then
- If sj(j, 7) < r Then
- Exit For
- Else
- Call dgH42(r - sj(j, 6), ri & "," & j, ra & sj(j, 1), rc & sj(j, 2), rv & sj(j, 3), j, t + 1)
- End If
- End If
- Next
- End Sub
复制代码
|