Need help for a mathematical problem

andrescane

New Member
Joined
Jan 20, 2004
Messages
3
First of all, sorry for my english. This is the problem: given some number and a group of other numbers, i need to calculate which of these number are the result of an addition that have as a result the first number. For example: result 100, group of numbers 20, 80, 3, 5. The correct answer is: 20 and 80.
do anybody know if this problem can be resolved in excel??? Thanks.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.

TryingToLearn

Well-known Member
Joined
Sep 10, 2003
Messages
723
Very dirty but seems to work:

Result goes in A1

list of numbers in A3 to A??

results are in message box and written to column c

Code:
Sub addit()
    Dim result, x, y As Integer
    Dim count, r, c

    r = "$C$1:" & Range("c65536").End(xlUp).Address
    Range(r).ClearContents
    With Sheets("sheet1")
        result = Cells(1, 1)
        count = Range("a3:a65536").End(xlDown).Offset(-2, 0).Row
        For x = 2 To count
            For y = 2 To count
                If Cells(x, 1).Address <> Cells(y, 1).Address Then
                    If Cells(x, 1) + Cells(y, 1) = result Then
                        MsgBox (" A1   =      A" & x & "  +   A" & y _
                              & Application.Rept(vbLf, 1) & result & "   =    " & Cells(x, 1).Value & "   +   " & Cells(y, 1).Value)
                        r = "A" & x & "  +   A" & y
                        Range("c3:c65536").End(xlUp).Offset(1, 0) = r

                    Else
                    End If
                End If
            Next y
        Next x
    End With
End Sub



Anyone care to clean it up ?
 

andrescane

New Member
Joined
Jan 20, 2004
Messages
3
Thanks, but it seems to be a problem. It appears an error (I am translate from spanish) "It has produced error '9' in execution time. Subindex out of interval" .
I think that the line is:
With Sheets("sheet1")
Can you fix it? I really thank you anyway
 

SIXTH SENSE

Well-known Member
Joined
Oct 29, 2003
Messages
1,883

ADVERTISEMENT

hi!
set up.
column A-numbers to search
D1 result

then try this one.

<font face=Courier New><SPAN style="color:#00007F">Sub</SPAN> Combinationx()
res = Range("d1").Value
<SPAN style="color:#00007F">For</SPAN> i = 2 <SPAN style="color:#00007F">To</SPAN> Range("a65536").End(xlUp).Row
    <SPAN style="color:#00007F">For</SPAN> j = 2 <SPAN style="color:#00007F">To</SPAN> Range("a65536").End(xlUp).Row
        <SPAN style="color:#00007F">If</SPAN> Range("a" & i).Value + Range("a" & j).Value = res <SPAN style="color:#00007F">Then</SPAN>
            Range("d" & Range("D65536").End(xlUp).Row + 1).Value = Range("a" & i).Value & "+" & Range("a" & j).Value & "=" & res
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    <SPAN style="color:#00007F">Next</SPAN> j
<SPAN style="color:#00007F">Next</SPAN> i
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 

andrescane

New Member
Joined
Jan 20, 2004
Messages
3
The macro that Sixth Sense left works, but only with two numbers. I need that it works with many more. For example: the result is 100 and the numbers are 10, 20, 60, 35, 56, 10. The correct numbers are 10+20+60+10
Can anybody give me the solution?
Thanks
 

CraigM

Active Member
Joined
Feb 27, 2003
Messages
320
My addin will do that for you. Also I believe the solver method will only give you the first solution that it "discovers". In reality there may be more than one solution. Again, the addin will give you all possible solutions.
 

PaddyD

MrExcel MVP
Joined
May 1, 2002
Messages
14,234
"Also I believe the solver method will only give you the first solution that it "discovers". "

Correct.
 

Yogi Anand

MrExcel MVP
Joined
Mar 12, 2002
Messages
11,454
Hi andrescane:

Welcome to MrExcel Board!

I was just having a little play with what you are trying to accomplish -- as presented in the following illustration ...
y040120h1a.xls
ABCDEFGHIJK
1Numberssumofanycombinationofnumbers=100
220successfulmatches
380sumofanysumofanysumofall
43twonumbersthreenumbersfournumbers
55000
6110000
700000
800
9
102080       
11     
12  
Sheet2


This formula based approach may not be of any practical use to you -- but you may find it to be of some interest.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,096
Messages
5,768,058
Members
425,451
Latest member
JohnBrooksBiddle

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
Top