find math expression with given elements and results

Fable09

New Member
Joined
Nov 15, 2014
Messages
36
Hi everyone,

How can this be solved in VBA

given S is a result and some number a b c d e f g

So how can i find the combination of math expression ( plus/ minus ) so that

a + b + c - d -e -f + g = S

or a - b +c - d - e +f - g = S

Thanks,
 
Last edited:

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I am not quite clear on what your expected outcome is.
Are you saying that you are given number a,b,c,d,e,f,g, and S, and one of the two equations you listed will be true, and if so, you want to know which of those formulas is true?
Or are you looking for something else?

It may be best to show us an actual example with your expected outcome.
 
Upvote 0
This seems related to a puzzle I've seen. Given the digits from 1-9, put operators (or nothing) between each digit to get a given result. For example, if you want to get a result of 2019, the answer would be:

1 + 2345 - 6 * 7 * 8 + 9 = 2019

In this version, the standard order of operations is used, although I've also seen it where it just expects the operations to be performed from left to right in order. I've written a macro that solves problems like this for arbitrary sums, and selectable operators. It would take a bit to make it work with arbitrary values as well.
 
Last edited:
Upvote 0
I do a lot of accounting reconciliation work so i'm looking for the vba code to work with this problem



Example GivenAfter running Macro
a
11

<tbody>
</tbody>
11

<tbody>
</tbody>
b22-22
c33-33
d44-44
e5555
f66-66
g77-77
S-176-176

<tbody>
</tbody>

As you can see , some how the macro calculate all possible cases and assign the PLUS or MINUS sign in front of each elements ( from a to g ) to get the given result -176

It would be great if you could help me using VBA to solve this
 
Last edited:
Upvote 0
Hi

I can't do this now, but just a remark: you may have more than 1 solution, like:

11 + 22 + 33 - 44 - 55 - 66 - 77 = -176
11 - 22 - 33 - 44 + 55 - 66 - 77 = -176
-11 + 22 - 33 + 44 - 55 - 66 - 77 = -176
-11 - 22 - 33 - 44 - 55 + 66 - 77 = -176

Not sure if it's important to your case.
 
Upvote 0
You can try the Solver. If you don't have it installed, click File > Options > Add-ins > Go (next to Excel Add-ins on the bottom) > and check the Solver box. Then set up your sheet like this:

Excel 2012
ABCD
1ItemValueSignWork
2a11-10
3b22-10
4c33-10
5d44-10
6e55-10
7f66-10
8g77-10
9
10
11S-308

<colgroup><col style="width: 25pxpx"><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet8

Worksheet Formulas
CellFormula
C2=IF(D2=0,-1,1)
B11=SUMPRODUCT(B2:B8,C2:C8)

<thead>
</thead><tbody>
</tbody>

<tbody>
</tbody>

Drag the C2 formula down to C8.

Then go to the Data tab, click Solver (far right), and enter these parameters:

Set Objective: $B$11
To: Value of: -176
By changing variable cells: $D$2:$D$8
Subject to the constraints: $D$2:$D$8 = binary
Solving method: Evolutionary

and click Solve. It'll ponder for a minute or so and hopefully come up with an answer.

Like pgc01 said, there are multiple solutions. And if you have a lot of values, this could take a long, long time. And if you want to include the option of not adding in a value at all, it gets even more complicated.
 
Upvote 0
If you don't have your heart set on a macro,

A​
B​
C​
D​
E​
F​
G​
H​
1​
11​
22​
33​
44​
55​
66​
77​
176​
2​
3​
a
b
c
d
e
f
g
Sum
6​
-1​
-1​
-1​
-1​
-1​
+1​
-1​
-176​
44​
-1​
+1​
-1​
+1​
-1​
-1​
-1​
-176​
72​
+1​
-1​
-1​
-1​
+1​
-1​
-1​
-176​
116​
+1​
+1​
+1​
-1​
-1​
-1​
-1​
-176​


In A4 and copied across and down,

=2*MOD(INT(2*(ROWS($G$4:Me) - 1) / 2 ^ COLUMNS($G$4:Me)), 2) - 1

In H4 and copied down,

=SUMPRODUCT($A$1:$G$1, A4:G4)

Filter col H for the desired sum.
 
Last edited:
Upvote 0
Thanks everyone for the answers. I probably need some time to digest the solutions

@shg: $G$4:Me , would you please help me understand where is range "Me" from the example ?
 
Upvote 0
Apologies, it's a named range that refers to the cell in which it appears. Change the formula in A4 to


=2*MOD(INT(2*(ROWS($G$4:A4) - 1) / 2 ^ COLUMNS($G$4:A4)), 2) - 1
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,821
Messages
6,121,759
Members
449,048
Latest member
excelknuckles

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