- Sub Solve(ByVal Total As Double, ByRef Data, Optional ByVal firstsolution As Boolean = True) 'Get the first( or all if the 3rd param is false) combintaions which has a sum as Total
- Dim Fit As Boolean, Count As Long, Result() As String, Used() As Byte, n As Long, Temp As Double 'Defines
- n = UBound(Data) 'Count of numbers-1
- ReDim Used(n)
- Do
- Fit = False 'Initialize
- Do
- For i = 0 To n
- Used(i) = 1 - Used(i) 'Used or Not used.
- If Used(i) = 1 Then Exit For
- Next
- If i > n Then Exit Do ' Nothing was found
- Temp = 0
- For i = 0 To n
- If Used(i) = 1 Then Temp = Temp + Data(i) 'Get the sum of used data
- Next
- If Abs(Temp - Total) < 0.01 Then 'be same
- Fit = True 'A solution has been found.
- Exit Do 'Quit a while.
- End If
- Loop
- If Fit Then 'Return the solution found just now.
- Count = Count + 1 'Solution count
- ReDim Preserve Result(1 To Count) 'Return the solution as an array.
- For i = 0 To n
- If Used(i) = 1 Then Result(Count) = Result(Count) & "+" & Data(i) 'The expression of the solution.
- Next
- Result(Count) = "Solution" & Count & ": " & Total & "=" & Mid(Result(Count), 2) 'message of solution
- Debug.Print Result(Count) 'Output to immediate window.
- If firstsolution = True Then MsgBox "1 solution has been found!!!": Exit Sub 'Need the first solution only.
- End If
- Loop While Fit
- MsgBox IIf(Count > 1, Count & " solutions have ", IIf(Count = 0, "No ", 1) & " solution has ") & " been found!!!" 'Three options of the result: 0,1 or many
- End Sub
- Sub Getit() 'Find combinations of an array which sums up to 1839.1
- Solve 1839.31, Array(466.89, 480.49, 281.06, 13.61, 70.59, 174.86, 313.82, 374.66, 281.5, 48.95, 262.86, 300.33, 248.56, 184.4, 442.91, 378.47, 196.49, 150.7, 162.87, 307.73, 64.97, 132.79, 480.06, 361.54, 240.13)
- End Sub
It returns:
Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49
If we change the codes as the following:
- Sub Getit() 'Find combinations of an array which sums up to 1839.1
- Solve 1839.31, Array(466.89, 480.49, 281.06, 13.61, 70.59, 174.86, 313.82, 374.66, 281.5, 48.95, 262.86, 300.33, 248.56, 184.4, 442.91, 378.47, 196.49, 150.7, 162.87, 307.73, 64.97, 132.79, 480.06, 361.54, 240.13),False
- End Sub
We'll get 29 solutions as the following:
Solution1: 1839.31=13.61+70.59+174.86+313.82+248.56+442.91+378.47+196.49
Solution2: 1839.31=466.89+281.06+13.61+70.59+281.5+378.47+196.49+150.7
Solution3: 1839.31=480.49+13.61+70.59+174.86+262.86+378.47+150.7+307.73
Solution4: 1839.31=281.06+13.61+70.59+174.86+313.82+281.5+48.95+196.49+150.7+307.73
Solution5: 1839.31=466.89+480.49+48.95+184.4+442.91+150.7+64.97
Solution6: 1839.31=480.49+13.61+174.86+313.82+281.5+196.49+150.7+162.87+64.97
Solution7: 1839.31=13.61+374.66+300.33+184.4+442.91+150.7+307.73+64.97
Solution8: 1839.31=13.61+313.82+48.95+300.33+248.56+378.47+162.87+307.73+64.97
Solution9: 1839.31=480.49+70.59+281.5+48.95+262.86+248.56+150.7+162.87+132.79
Solution10: 1839.31=281.06+13.61+70.59+281.5+300.33+184.4+196.49+150.7+162.87+64.97+132.79
Solution11: 1839.31=281.06+262.86+248.56+378.47+162.87+307.73+64.97+132.79
Solution12: 1839.31=466.89+70.59+174.86+48.95+262.86+184.4+150.7+480.06
Solution13: 1839.31=281.06+13.61+70.59+281.5+300.33+196.49+150.7+64.97+480.06
Solution14: 1839.31=281.06+13.61+174.86+262.86+300.33+248.56+196.49+361.54
Solution15: 1839.31=281.06+13.61+174.86+313.82+248.56+184.4+196.49+64.97+361.54
Solution16: 1839.31=70.59+174.86+281.5+48.95+378.47+150.7+307.73+64.97+361.54
Solution17: 1839.31=13.61+70.59+442.91+162.87+307.73+480.06+361.54
Solution18: 1839.31=70.59+48.95+300.33+248.56+196.49+132.79+480.06+361.54
Solution19: 1839.31=466.89+281.06+70.59+174.86+442.91+162.87+240.13
Solution20: 1839.31=374.66+281.5+248.56+184.4+196.49+150.7+162.87+240.13
Solution21: 1839.31=281.06+13.61+174.86+374.66+262.86+184.4+307.73+240.13
Solution22: 1839.31=466.89+70.59+174.86+48.95+262.86+196.49+150.7+162.87+64.97+240.13
Solution23: 1839.31=281.06+374.66+281.5+378.47+150.7+132.79+240.13
Solution24: 1839.31=70.59+374.66+48.95+184.4+307.73+132.79+480.06+240.13
Solution25: 1839.31=281.06+13.61+174.86+48.95+248.56+162.87+307.73+361.54+240.13
Solution26: 1839.31=480.49+13.61+70.59+174.86+300.33+64.97+132.79+361.54+240.13
Solution27: 1839.31=13.61+281.5+48.95+262.86+248.56+184.4+64.97+132.79+361.54+240.13
Solution28: 1839.31=313.82+262.86+300.33+162.87+64.97+132.79+361.54+240.13
Solution29: 1839.31=281.06+70.59+374.66+150.7+162.87+64.97+132.79+361.54+240.13