Solved

Goal Seek - List of Numbers

Posted on 2013-02-04
6
374 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 11

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
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.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

746 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

13 Experts available now in Live!

Get 1:1 Help Now