extracting numbers to equal variable number

Cloverken

Active Member
Joined
Feb 18, 2002
Messages
271
Hi,

I have 4 columns with 6 numbers in each column in separate cells. I have a variable number in the 5th column in one cell. Is it possible to list the ways to equal that variable number by extracting one number from each column by summing them and then listing them below the variable number? If there is no comibination of numbers to equal variable, then it would just be a message stating that.

I feel like it is asking a lot. Hopefully it can be done.

Thanks
Ken
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
Something like this will work. Although this is only making calculations by removing one number. There may be viable combinations by removing more than one number, but that would be much harder.

Code:
Sub Test()

Dim Sum1            As Double
Dim Sum2            As Double
Dim Sum3            As Double
Dim Sum4            As Double
Dim Sum5            As Double
Dim Sum6            As Double

    Sum1 = Range("A3").Value + Range("A4").Value + Range("A5").Value + Range("A6").Value + Range("A7").Value
    Sum2 = Range("A2").Value + Range("A4").Value + Range("A5").Value + Range("A6").Value + Range("A7").Value
    Sum3 = Range("A2").Value + Range("A3").Value + Range("A5").Value + Range("A6").Value + Range("A7").Value
    Sum4 = Range("A2").Value + Range("A3").Value + Range("A4").Value + Range("A6").Value + Range("A7").Value
    Sum5 = Range("A2").Value + Range("A3").Value + Range("A4").Value + Range("A5").Value + Range("A7").Value
    Sum6 = Range("A2").Value + Range("A3").Value + Range("A4").Value + Range("A5").Value + Range("A6").Value
    Select Case True
        Case Sum1 = Range("A8").Value
            Range("A9").Value = Range("A2").Value
        Case Sum2 = Range("A8").Value
            Range("A9").Value = Range("A3").Value
        Case Sum3 = Range("A8").Value
            Range("A9").Value = Range("A4").Value
        Case Sum4 = Range("A8").Value
            Range("A9").Value = Range("A5").Value
        Case Sum5 = Range("A8").Value
            Range("A9").Value = Range("A6").Value
        Case Sum6 = Range("A8").Value
            Range("A9").Value = Range("A7").Value
        Case Else
            MsgBox "Sorry No Match", vbInformation
    End Select

End Sub

The data in this example is in A2:A7
Number to match is in A8
Number to remove is placed in A9
 
Upvote 0
Hi!
Is this what you want?
comb-perm2.xls
ABCDEF
1listDesiredSum10possiblecombinationSum
21Summary4+6=10
32numberofresults52+3+5=10
43GREATERTHAN10%01+4+5=10
54LESSTHAN10%01+3+6=10
65EQUALTO51+2+3+4=10
76TOTAL5
8
9
10
11
12
13
14
Sheet5


The code!<font face=Courier New><SPAN style="color:#00007F">Option</SPAN><SPAN style="color:#00007F">Explicit</SPAN><SPAN style="color:#00007F">Dim</SPAN> n, k, q<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Double</SPAN><SPAN style="color:#00007F">Dim</SPAN> Comblist()<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN><SPAN style="color:#00007F">Dim</SPAN> b()<SPAN style="color:#00007F">Dim</SPAN> Ans()<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Variant</SPAN><SPAN style="color:#00007F">Private</SPAN><SPAN style="color:#00007F">Sub</SPAN> Comb(j, m<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">Integer</SPAN>)<SPAN style="color:#00007F">Dim</SPAN> tmp, X
    <SPAN style="color:#00007F">If</SPAN> j > n<SPAN style="color:#00007F">Then</SPAN>
        tmp = ""
        <SPAN style="color:#00007F">For</SPAN> X = 1<SPAN style="color:#00007F">To</SPAN> n
            tmp = tmp & b(X)
        <SPAN style="color:#00007F">Next</SPAN> X
        
        q = q + 1
        <SPAN style="color:#00007F">ReDim</SPAN><SPAN style="color:#00007F">Preserve</SPAN> Comblist(UBound(Comblist) + 1)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN>
        Comblist(UBound(Comblist)) = tmp

    <SPAN style="color:#00007F">Else</SPAN>
        <SPAN style="color:#00007F">If</SPAN> k - m< n - j + 1<SPAN style="color:#00007F">Then</SPAN>
            b(j) = 0
            Comb j + 1, m
        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
        <SPAN style="color:#00007F">If</SPAN> m< k<SPAN style="color:#00007F">Then</SPAN>
            b(j) = 1
            Comb j + 1, m + 1
        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN><SPAN style="color:#00007F">Sub</SPAN> Sumxxx()<SPAN style="color:#00007F">Dim</SPAN> St, i, j, tmp, Tmp2, AnsCount, L, Res<SPAN style="color:#00007F">Dim</SPAN> listx()
Application.ScreenUpdating =<SPAN style="color:#00007F">False</SPAN>
Application.Calculation = xlCalculationManual
n = Range("a65536").End(xlUp).Row - 1
Res = Range("c1").Value
q = 0<SPAN style="color:#00007F">ReDim</SPAN> Comblist(0)<SPAN style="color:#00007F">As</SPAN><SPAN style="color:#00007F">String</SPAN><SPAN style="color:#00007F">ReDim</SPAN> b(n)<SPAN style="color:#00007F">ReDim</SPAN> listx(n)<SPAN style="color:#00007F">ReDim</SPAN> Ans(0)<SPAN style="color:#00007F">For</SPAN> k = 1<SPAN style="color:#00007F">To</SPAN> n
Comb 1, 0<SPAN style="color:#00007F">Next</SPAN> k<SPAN style="color:#00007F">For</SPAN> i = 1<SPAN style="color:#00007F">To</SPAN> n
    listx(i) = Range("a" & 1 + i).Value<SPAN style="color:#00007F">Next</SPAN> i
AnsCount = 0<SPAN style="color:#00007F">For</SPAN> i =<SPAN style="color:#00007F">LBound</SPAN>(Comblist)<SPAN style="color:#00007F">To</SPAN><SPAN style="color:#00007F">UBound</SPAN>(Comblist)
    St = ""
    tmp = 0
    <SPAN style="color:#00007F">For</SPAN> j = 1<SPAN style="color:#00007F">To</SPAN> Len(Comblist(i))
        tmp = tmp + Mid(Comblist(i), j, 1) * listx(j)
        <SPAN style="color:#00007F">If</SPAN> Mid(Comblist(i), j, 1) = 1<SPAN style="color:#00007F">Then</SPAN>
            St = St & "+" & listx(j)
        <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> j
    <SPAN style="color:#00007F">If</SPAN> tmp = Res<SPAN style="color:#00007F">Then</SPAN><SPAN style="color:#007F00">'Or (tmp >= 0.9 * Res And tmp<= 1.1 * Res) Then</SPAN>
        <SPAN style="color:#00007F">ReDim</SPAN><SPAN style="color:#00007F">Preserve</SPAN> Ans(UBound(Ans) + 1)
        AnsCount = AnsCount + 1
        Ans(AnsCount) = St
    <SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">If</SPAN><SPAN style="color:#00007F">Next</SPAN> i
Range("e2:f65536").Value = ""<SPAN style="color:#00007F">For</SPAN> i =<SPAN style="color:#00007F">LBound</SPAN>(Ans) + 1<SPAN style="color:#00007F">To</SPAN><SPAN style="color:#00007F">UBound</SPAN>(Ans)
    Range("e" & i + 1).Value = "'" & Mid(Ans(i), 2, Len(Ans(i))) & "="
    Range("F" & i + 1).Value = "=" & Ans(i)<SPAN style="color:#00007F">Next</SPAN> i
Application.ScreenUpdating =<SPAN style="color:#00007F">True</SPAN>
Application.Calculation = xlCalculationAutomatic<SPAN style="color:#00007F">End</SPAN><SPAN style="color:#00007F">Sub</SPAN></FONT>

Note:
The above solution is not limited to 6 possible combination. this is a general solution for permuation problems. you may take up to 20 individual number in the list but would take sometimes to return.Above 20 may take you several minutes or give you an error of "stack overflow"
because of recursive nature of the function above.
 
Upvote 0
Hello Ken

You might like to try this:

First set up your page with numbers by running this macro code:

Sub SetUp()
Range("A1:D6").FormulaR1C1 = "=ROW()+COLUMN()"
Range("A1:D6").Copy
Range("A1:D6").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Application.CutCopyMode = False
Range("E1").Value = 20
Range("A1").Select
End Sub

Now paste the following code into a standard macro module.
This macro will sum one number from each column and if the combination equals the value in E1 (20 in my example), it will display those 4 numbers across columns F,G,H,I (in the example given that is 80 different combinations)

Sub Combinations()
Application.ScreenUpdating = False
For Each cell1 In Range("A1:A6")
For Each cell2 In Range("B1:B6")
For Each cell3 In Range("C1:C6")
For Each cell4 In Range("D1:D6")
If cell1.Value + cell2.Value + cell3.Value + cell4.Value = [e1].Value Then
Range("F65536").End(xlUp).Offset(1, 0).Value = cell1.Value
Range("G65536").End(xlUp).Offset(1, 0).Value = cell2.Value
Range("H65536").End(xlUp).Offset(1, 0).Value = cell3.Value
Range("I65536").End(xlUp).Offset(1, 0).Value = cell4.Value
End If
Next
Next
Next
Next
End Sub

regards
Derek
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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