= = = =
As a side note on your project:
> .. result = Total@{a+c+d}
You already have the total with "a+c+d".
Perhaps you meant:
result = Total@{a, c, d}
result = {a, c, d} // Tr
I would recommend making it a little "easier" to read:
result /. Thread[{KA, KB, KD, KT} -> KZ]
= = = =
Hi Dana. You're of course correct that the Total command in my Mathematica example is redundant; I've updated the screenshot to eliminate that. Thanks.
Hi. My thoughts are this would be hard to do if you change the basic input equations.
As is, you might be able to go the vba route.
For example, we could extract the following from 1 equation:
Sub Demo()
Dim s, v
s = Mid([A1].Formula, 2) ' Remove =
v = Split(s, "+")
Debug.Print v(0)
Debug.Print v(1)
Debug.Print v(2)
End Sub
returns:
KA*40%
KD*10%
LH*45%
You could then keep track of the values as they are added.
Then join the string parts.
However, this would only apply if the equations follow a specific pattern.
The equations will change, but they will always follow the same pattern:
XX*##% + XX*##% + XX*##% +....
where X is any letter, and # is any digit.
I've done programming (C++, Mathematica, Perl), but never used VBA. Nevertheless, I can see from your example how VBA could be programmed to do text replacement. But could it then collect terms and sum them?
I.e., once it extracts, say, AB*45%, AB*55%, CD*80%, CD*15%, how would it know to add them to give AB*100%, CD*95%? That last part is the heart of what I need to do.
If I only needed to display all the individual terms, I can do that already, by converting each equation to a string, using FORMULATEXT. For instance, the following (using what I've indicated is in cell B3, above):
=IF(B3<>0,SUBSTITUTE(FORMULATEXT(B3),"=",""),"")
gives:
KA*40% + KD*10% + LH*45%
While this:
=IF(B3<>0,SUBSTITUTE(FORMULATEXT(B3),"+",", "),"")
gives:
=KA*40%, KD*10%, LH*45%
I can then combine the string versions for all the cells (B3 through E3) using CONCATENATE.
Finally, using SUBSTITUTE, I could clean up these strings to give either:
KZ*40% + KZ*10% + LH*45%+ KZ*5% + KZ*40% + GV*25% + LH*55%
or:
KZ*40%, KZ*10%, LH*45%, KZ*5%, KZ*40%, GV*25%, LH*55%
But how do I convert these strings back to formulas and get Excel to do algebra on them? I.e., how do I get it to transform the above to either:
GV*25% + KZ*95%+ LH*100%
or:
GV*25%, KZ*95%, LH*100%
Can VBA do simple symbolic algebra or, like Excel, is it limited to numerical calculations?
I looked through a list of all VBA functions here (https://vbaf1.com/functions/#\_Toc5), and it seems the answer is no: that VBA cannot do simple symbolic algebra, it can only do numerical calculations.