?
Solved

Goal Seek - List of Numbers

Posted on 2013-02-04
6
Medium Priority
?
474 Views
Last Modified: 2013-02-05
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
Comment
Question by:Glenn Stearns
[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
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 24

Expert Comment

by:Steve
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

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

Author Comment

by:Glenn Stearns
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 24

Accepted Solution

by:
Steve earned 2000 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

Open in new window


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

by:Glenn Stearns
ID: 38856273
Works perfectly! Thanks so much. We will get much use from this little program!
0
 
LVL 24

Expert Comment

by:Steve
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question