[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Goal Seek - List of Numbers

Posted on 2013-02-04
6
Medium Priority
?
497 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

656 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