Hello,
I have this VBA (below) which will allow the summing of a list of numbers to reach a stated number.
When using =getcombination(A2:A9,C2) for example, where A2:A9 is a list of numbers, and C2 is the desired sum
However, the result is only 1 possible combination (and usually not a desired one). Is there a way to modify the VBA so the 1 combination shown is the result with the fewest number of combinations?
I have this VBA (below) which will allow the summing of a list of numbers to reach a stated number.
When using =getcombination(A2:A9,C2) for example, where A2:A9 is a list of numbers, and C2 is the desired sum
However, the result is only 1 possible combination (and usually not a desired one). Is there a way to modify the VBA so the 1 combination shown is the result with the fewest number of combinations?
VBA Code:
Function GetCombination(CoinsRange As Range, SumCellId As Double) As String
Dim xStr As String
Dim xSum As Double
Dim xCell As Range
xSum = SumCellId
For Each xCell In CoinsRange
If Not (xSum / xCell < 1) Then
xStr = xStr & Int(xSum / xCell) & " of " & xCell & " "
xSum = xSum - (Int(xSum / xCell)) * xCell
End If
Next
GetCombination = xStr
End Function
Last edited by a moderator: