Excel formula - urgent help needed!

smalldj

New Member
Joined
Apr 2, 2003
Messages
8
If you have a long list of numbers - and you want to find one particular total of these numbers, can a formula be written that shows all the possible combinations of numbers that will make up the total you are looking for?

Sounds quite mumbled and jumbled but hopefully you guys know what i am on about!

thank you
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
25.06
264.32
40.96
54.32
68889.14
2.00
1.00
1.00
5.00
67779.21
40.95
0.01

Say i wanted to find all the possible combinations for 67820.17 from the list above.

Is it possible to write a formula that will show me this?
 
Upvote 0
Here is a macro that will put in column C the combinations of values in column A that equal the value in cell B1 (source unknown):

Code:
Option Explicit

'These private variables are used by the AddsUp macro
Private Target As Double ' The target total we're aiming for
Private EndRow As Integer ' The last row of the value list
Private Limit As Integer  ' sum no more than this many cells
Private OutRow As Integer ' The row for the next output line

Sub AddsUp()
'   *** Results in column C - change to suit ***
    Columns(3).Clear
'   *** Required answer - change reference to suit ***
    Target = Range("B1").Value
'   *** The last row in the list of values - change Range reference to suit ***
    EndRow = Range("A1").End(xlDown).Row
'   You can change the next two values
    Limit = 10       ' Max number of cells to be summed
    OutRow = 1      ' The row for the next output line
'   You can change the first argument in the function call that follows.
'   Doing so will change the starting row. Do not change the other
'   three arguments
    Add1 1, 0, "", 0
End Sub

Private Sub Add1(ByVal BegRow As Integer, ByVal SumSoFar As Double, _
  ByVal OutSoFar As String, ByVal Num As Integer)
'This subroutine is called once by the AddsUp macro, to get the process
'started. It then calls itself recursively as many times as needed.
'
'BegRow - the first row that will be tested
'SumSoFar - the sum of all cells under consideration
'OutSoFar - the addresses of all cells under consideration
'Num - the number of cells under consideration
    Dim ThisRow As Long
    Dim OneA As String
    If (BegRow <= EndRow) And (SumSoFar < Target) And (Num < Limit) Then
        For ThisRow = BegRow To EndRow
            OneA = Cells(ThisRow, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)
            If OutSoFar <> "" Then
                OneA = " + " & OneA
            End If
'           If the current cell's value plus the sum so far equals
'           the target, then we have found an answer. Display it
'           in the current output row, and set OutRow to the next row
            If (Round(SumSoFar + Cells(ThisRow, 1).Value, 2) = Target) And (Num > 0) Then
                Cells(OutRow, 3).Value = OutSoFar & OneA
                OutRow = OutRow + 1
            Else
'           If the current cell's value plus the sum so far does not
'           equal the target value, call this function again, starting
'           in the row after ThisRow
                Add1 ThisRow + 1, Round(SumSoFar + Cells(ThisRow, 1).Value, 2), _
                OutSoFar & OneA, Num + 1
            End If
        Next ThisRow
    End If
End Sub
 
Upvote 0
Here is a macro that will put in column C the combinations of values in column A that equal the value in cell B1 (source unknown):

Code:
Option Explicit
 
'These private variables are used by the AddsUp macro
Private Target As Double ' The target total we're aiming for
Private EndRow As Integer ' The last row of the value list
Private Limit As Integer  ' sum no more than this many cells
Private OutRow As Integer ' The row for the next output line
 
Sub AddsUp()
'   *** Results in column C - change to suit ***
    Columns(3).Clear
'   *** Required answer - change reference to suit ***
    Target = Range("B1").Value
'   *** The last row in the list of values - change Range reference to suit ***
    EndRow = Range("A1").End(xlDown).Row
'   You can change the next two values
    Limit = 10       ' Max number of cells to be summed
    OutRow = 1      ' The row for the next output line
'   You can change the first argument in the function call that follows.
'   Doing so will change the starting row. Do not change the other
'   three arguments
    Add1 1, 0, "", 0
End Sub
 
Private Sub Add1(ByVal BegRow As Integer, ByVal SumSoFar As Double, _
  ByVal OutSoFar As String, ByVal Num As Integer)
'This subroutine is called once by the AddsUp macro, to get the process
'started. It then calls itself recursively as many times as needed.
'
'BegRow - the first row that will be tested
'SumSoFar - the sum of all cells under consideration
'OutSoFar - the addresses of all cells under consideration
'Num - the number of cells under consideration
    Dim ThisRow As Long
    Dim OneA As String
    If (BegRow <= EndRow) And (SumSoFar < Target) And (Num < Limit) Then
        For ThisRow = BegRow To EndRow
            OneA = Cells(ThisRow, 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)
            If OutSoFar <> "" Then
                OneA = " + " & OneA
            End If
'           If the current cell's value plus the sum so far equals
'           the target, then we have found an answer. Display it
'           in the current output row, and set OutRow to the next row
            If (Round(SumSoFar + Cells(ThisRow, 1).Value, 2) = Target) And (Num > 0) Then
                Cells(OutRow, 3).Value = OutSoFar & OneA
                OutRow = OutRow + 1
            Else
'           If the current cell's value plus the sum so far does not
'           equal the target value, call this function again, starting
'           in the row after ThisRow
                Add1 ThisRow + 1, Round(SumSoFar + Cells(ThisRow, 1).Value, 2), _
                OutSoFar & OneA, Num + 1
            End If
        Next ThisRow
    End If
End Sub

This is awesome and is exactly what I was looking for!

This place is awesome for resources!
 
Upvote 0

Forum statistics

Threads
1,224,503
Messages
6,179,136
Members
452,890
Latest member
Nikhil Ramesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top