Is there a clever way using VBA or a formula to find “k” and “m” variables in a kx+m string?
There are several scenarios for how the kx+m string can look, e.g.:
312*x+12
12+x*2
-4-x
and so on. I’m pretty sure I can solve this by writing very complicating formulas in Excel, but I’m thinking maybe someone has already solved this and similar problems. Here is my best shot so far, but it doesn’t handle all situations yet (like when there are two minuses in the kx+m string:
=TRIM(IF(NOT(ISERROR(SEARCH("~+";F5)));
IF(SEARCH("~+";F5)>SEARCH("~*";F5);RIGHT(F5;LEN(F5)-SEARCH("~+";F5));LEFT(F5;SEARCH("~+";F5)-1));
IF(NOT(ISERROR(SEARCH("~-";F5)));
IF(SEARCH("~-";F5)>SEARCH("~*";F5);RIGHT(F5;LEN(F5)-SEARCH("~-";F5)+1);LEFT(F5;SEARCH("~*";F5)-1));"")))
I’m sure this will help you 🙂
Put this function in a Module:
Then you can either call it from a Macro
e.g. like this:
Or use it like a formula.
e.g. by putting this in a cell:
I like the second way (less work :P)
I tested it with various values and here’s a screenshot of what I get:
Hope this helps 🙂