Solved

Goal Seek - List of Numbers

Posted on 2013-02-04
6
389 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:glennes
  • 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:glennes
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 24

Accepted Solution

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

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:glennes
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
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…

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now