|
本帖最后由 astupig 于 2020-1-17 16:14 编辑
确保A列是排序好的. 点击按钮就可以凑了.
就是用VBA调用一下规划求解(Solver)就可以了.
很多凑不到的. 我目前帮你循环了前面30个, 要多一些范围你自己改一下范围就可以.
- Sub Macro1()
- '
- arr = [a1:a28738]
- brr = [g1:g2268]
-
- For i = 2 To 30
- c1 = 0
- c2 = 0
-
- For t = 2 To UBound(arr)
- If arr(t, 1) = brr(i, 1) Then
- c1 = c1 + 1
- sR = t
- Exit For
- End If
- Next t
-
- For t = UBound(arr) To 2 Step -1
- If arr(t, 1) = brr(i, 1) Then
- c2 = c2 + 1
- eR = t
- Exit For
- End If
- Next
- If c1 = 0 Or sR = eR Then
- sR = 65000
- eR = 65001
- End If
- Range("I" & i).Formula = "=Sumproduct(" & Range(Cells(sR, "b"), Cells(eR, "b")).Address & "," & Range(Cells(sR, "c"), Cells(eR, "c")).Address & ")"
- SolverReset
- SolverOk SetCell:="$I$" & i, MaxMinVal:=3, ValueOf:=Range("H" & i).Value, ByChange:=Range(Cells(sR, "c"), Cells(eR, "c")).Address, _
- Engine:=2, EngineDesc:="Simplex LP"
- SolverAdd CellRef:=Range(Cells(sR, "c"), Cells(eR, "c")).Address, Relation:=5, FormulaText:="binary"
- SolverOk SetCell:="$I$" & i, MaxMinVal:=3, ValueOf:=Range("H" & i).Value, ByChange:=Range(Cells(sR, "c"), Cells(eR, "c")).Address, _
- Engine:=2, EngineDesc:="Simplex LP"
- SolverOk SetCell:="$I$" & i, MaxMinVal:=3, ValueOf:=Range("H" & i).Value, ByChange:=Range(Cells(sR, "c"), Cells(eR, "c")).Address, _
- Engine:=2, EngineDesc:="Simplex LP"
- SolverSolve UserFinish:=True
-
- If Cells(i, "h") <> Cells(i, "i") Then
- Cells(i, "i").Value = "找不到"
- End If
-
- Next i
-
- End Sub
复制代码
|
|