How to find the combination of numbers that equal to value n

Luthius

Active Member
Joined
Apr 5, 2011
Messages
324
Hello guys
I found a very usefull code and I would like your assistance on implement it on limitating the quantity of numbers that will give the desired sum.
For instance I would like the combination of numbers limitating to 13 the set of numbers.
So I will have for each set of numbers 13 numbers as example.
I will choose the quantity of sets and also the quantity of number in each set that will give me as sum result my desired value.

Code:
Option Explicit 
Function RealEqual(A, B, Epsilon As Double)
    RealEqual = Abs(A - B) <= Epsilon
End Function
Function ExtendRslt(CurrRslt, NewVal, Separator)
    If CurrRslt = "" Then ExtendRslt = NewVal _
Else ExtendRslt = CurrRslt & Separator & NewVal
End Function
 
Sub recursiveMatch(ByVal MaxSoln As Integer, ByVal TargetVal, InArr(), _
    ByVal CurrIdx As Integer, _
    ByVal CurrTotal, ByVal Epsilon As Double, _
    ByRef Rslt(), ByVal CurrRslt As String, ByVal Separator As String)
    Dim I As Integer
    For I = CurrIdx To UBound(InArr)
        If RealEqual(CurrTotal + InArr(I), TargetVal, Epsilon) Then
            Rslt(UBound(Rslt)) = (CurrTotal + InArr(I)) _
            & Separator & Format(Now(), "hh:mm:ss") _
            & Separator & ExtendRslt(CurrRslt, I, Separator)
            If MaxSoln = 0 Then
                If UBound(Rslt) Mod 100 = 0 Then Debug.Print UBound(Rslt) & "=" & Rslt(UBound(Rslt))
            Else
                If UBound(Rslt) >= MaxSoln Then Exit Sub
            End If
            ReDim Preserve Rslt(UBound(Rslt) + 1)
        ElseIf CurrTotal + InArr(I) > TargetVal + Epsilon Then
        ElseIf CurrIdx < UBound(InArr) Then
            recursiveMatch MaxSoln, TargetVal, InArr(), I + 1, _
            CurrTotal + InArr(I), Epsilon, Rslt(), _
            ExtendRslt(CurrRslt, I, Separator), _
            Separator
            If MaxSoln <> 0 Then If UBound(Rslt) >= MaxSoln Then Exit Sub
        Else
             'we've run out of possible elements and we _
            still don 't have a match
        End If
    Next I
End Sub
 
Function ArrLen(Arr()) As Integer
    On Error Resume Next
    ArrLen = UBound(Arr) - LBound(Arr) + 1
End Function

Source:
HTML:
http://www.tushar-mehta.com/excel/templates/match_values/
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Below the code that will get the result, but I would like to have only the array with specific quantity of numbers. For instance array with 12 numbers only or other value that I will specify.
MaxSoln= Quantity of arrays that the sum results the TargetVal
TargetVal= My objective.

As mentioned before, I would like only N numbers in my array.

TargetVal=100
MaxSoln= 30 set of number
Number_on_Array=12

Result:
{1-2-3-4-5-6-7-8-9-10-22-23}
{1-2-3-4-5-6-7-8-9-13-17-25}
{...}
How to implement this change in my code??
Code:
Sub GenerateNumbers()Dim i As Integer
Dim TargetVal, Rslt(), InArr(), StartTime As Date, MaxSoln As Integer
    MaxSoln = Selection.Cells(1).Value
    TargetVal = Selection.Cells(2).Value
    InArr = Application.WorksheetFunction.Transpose( _
    Selection.Offset(2, 0).Resize(Selection.Rows.Count - 2).Value)
    ReDim Rslt(0)
    recursiveMatch MaxSoln, TargetVal, InArr, LBound(InArr), 0, 0.00000001, _
    Rslt, "", "-"
    ReDim Preserve Rslt(UBound(Rslt) + 1)
    Selection.Offset(0, 1).Resize(ArrLen(Rslt), 1).Value = _
    Application.WorksheetFunction.Transpose(Rslt)
End Sub
 
Upvote 0
How many numbers do you have?
 
Upvote 0
The numbers in column A are all different (no repeats)?

And if so, are they always sequential (not necessarily sorted, but sequential)
 
Upvote 0
No repeats, for instance in column A
A1=30 (My sets of arrays to show)
A2=100 (The result of the sum of each set. My target)
A3=12 (Max numbers in each array)
A4=1
A5=2
A6=(...) Continue sequentially
A28=25

It means that from A4 to A18 will have some numbers. In this example from 01 to 25.
 
Upvote 0
In that case, there's no need for the numbers. See the workbook at https://app.box.com/s/qslxd47xo3olfc33rzmiwbdyz54vnooh

For your example, there are 30 possible results:

Partitions of 100 into 13 distinct numbers
22 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
21 + 13 + 11 + 10 + 9 + 8 + 7 + 6 + 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+ 13 + 12 + 10 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1
17 + 13 + 12 + 11 + 10 + 9 + 7 + 6 + 5 + 4 + 3 + 2 + 1
16 + 14 + 12 + 11 + 10 + 9 + 7 + 6 + 5 + 4 + 3 + 2 + 1
15 + 14 + 13 + 11 + 10 + 9 + 7 + 6 + 5 + 4 + 3 + 2 + 1
16 + 13 + 12 + 11 + 10 + 9 + 8 + 6 + 5 + 4 + 3 + 2 + 1
15 + 14 + 12 + 11 + 10 + 9 + 8 + 6 + 5 + 4 + 3 + 2 + 1
15 + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 5 + 4 + 3 + 2 + 1
14 + 13 + 12 + 11 + 10 + 9 + 8 + 7 + 6 + 4 + 3 + 2 + 1
 
Upvote 0
It downloads fine for me, and there's more code in the workbook than I want to post.
 
Upvote 0
I perform some tests but nothing is coming.... Well I will study it and see if I can get some success...

Thank you for your help
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,170
Messages
6,123,422
Members
449,099
Latest member
COOT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top