Solved

Goal Seek - List of Numbers

Posted on 2013-02-04
442 Views
Is there a way to use goal seeking (or some other Excel function) to analyze a column of numbers and find out which ones of those numbers add up to a certain total amount? These are very varied dollar amounts and none are the same, so there is only one sub-set of the numbers in the column that would equal the sum I am looking for them to equal. Can Excel do this, and, if so, how do you do it?
0
Question by:glennes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 24

Expert Comment

ID: 38853971
Ah, the old unballanced accounting entries, such fun.

There is no in-built Excel function to do this for you.
It would be possible to build a VBA program to do this.

But the problem is that the code could become very slow using full itteration.
This being where we start at row 1 and add row 2 to it... then add row 1 to 3... then row 1 to 4.
This could potentially take a huge amount of time over large data sets (which you are likely to have or the answer would no doubt be obvious).

So it would come down to some "cunning and clever" code.
So for example ...
starting by eliminating all values bigger than the target.
Then taking each big number and looking for a small one to make up the total.
But after this (looking for a two number combination) the three or more number combination just adds more and more time.

Would you have an example of the list of numbers (just the numbers will be fine for testing) and the target, we can then have a go at getting creative with some code.
Though my biggest concern is that as you get to a combination of 5 numbers to form the target, the time taken to run the code may get excessive. Will have to test to see.
0

LVL 12

Expert Comment

ID: 38854133
How many numbers are in the column, approximately, glennes?
0

Author Comment

ID: 38854720
I have attached the list of numbers as a worksheet below. The target sum is \$45,409.27.
Thanks so much!
List-of-numbers.xlsx
0

LVL 24

Accepted Solution

Steve earned 500 total points
ID: 38855877
OK, the attached worksheet has the following code:

``````Sub itterate()

Dim goal As Double: goal = ThisWorkbook.Worksheets("2").Range("B1").Value

Dim myArr: myArr = ThisWorkbook.Worksheets("2").Range("A3:A83").Value

Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim e As Integer
Dim f As Integer
Dim x As Double

For a = 1 To 81
Debug.Print a
For b = a To 81
For c = b To 81
For d = c To 81
For e = d To 81
For f = e To 81
x = myArr(a, 1) + myArr(b, 1) + myArr(c, 1) + myArr(d, 1) + myArr(e, 1) + myArr(f, 1)
If x = goal Then
MsgBox (myArr(a, 1) & vbCrLf & myArr(b, 1) & vbCrLf & myArr(c, 1) & vbCrLf & myArr(d, 1) & vbCrLf & myArr(e, 1) & vbCrLf & myArr(f, 1))
Exit Sub
End If
Next f
Next e
Next d
Next c
Next b
Next a

End Sub
``````

It is "slow" as it does not have any real inteligence, just a pure cycle requiring 6 itterations to get the answer.

See attached.
List-of-numbers.xlsm
0

Author Closing Comment

ID: 38856273
Works perfectly! Thanks so much. We will get much use from this little program!
0

LVL 24

Expert Comment

ID: 38857297
Glad to be able to assist... as I say, this is slow to run (due to full itteration method) especially over large numbers of data rows.

So if it needs to be changed to speed it up let us know. We love a challenge :)
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (â€śforceâ€ť) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;â€¦
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaacâ€¦
Suggested Courses
Course of the Month8 days, 21 hours left to enroll