0

I am currently working on a listing template that uses different things like a title generator. This generator uses a ton of if statements based on the data that it is given. I have broken this generator up into different functions for each data type so that I can edit these individual functions for later use.

What I am having a tough time doing is creating an algorithm that references a formula in a different cell. I would like this algorithm to execute a specific formula based on the data that is given. For example, I can have my data type in column A and the data formulas in column B. For each item in column A, there is a unique formula for it in column B. This is what I have in my functions sheet. In a different sheet, lets call it data sheet, I have a table that contains a lot of different data. One column is data type, and it contains the same data types as in my formulas sheet. In this sheet, I would like to execute the exact formula in the formulas sheet based on the data type that I have in this data sheet.

I used formulatext() and it returns the formula that I need. My problem is that I would like to execute this formula instead of receiving a long string of it.

3
  • There is no regex or evaluate formula in excel, but there is in VBA. I believer you can use VBA to create a UDF to end up with what you want.
    – gns100
    Feb 24, 2021 at 23:47
  • Please edit you post & share some sample data with us, as well the formula you are trying so far !! Feb 25, 2021 at 4:52
  • Try to provide a sample about your problem and it will be more helpful.
    – Lee
    Feb 25, 2021 at 9:31

1 Answer 1

0

If you are willing to believe me that a thing exists long enough to actually try it, there is at least one way.

Before VBA, Excel had a macro language. When they added VBA to the program, they kept the old macro language. People supposed it was for backward compatibility and that it would, as hinted, disappear in some future version. Well, it never disappeared. It's actually fully functional and the who shebang is there if one wants to activate it.

You don't need "the whole shebang" though and very happily, those functions it had that have meaning, so to speak, without using the full package, work even today. In fact, there's some evidence in MS's online aids that it can never go away, so it might exist forever and everything work like it does 25 years after they began saying it might go away.

The thing is though, they do not work outside either (your choice) actual Excel 4 macros (the whole shebang installed, and building macro pages with it) OR inside Named Ranges for the functions that Excel can make sense of outside their macro purposes.

EVALUATE() is the function of interest here. It can do to a formulaic string (any string that would make sense if it were entered after an = what INDIRECT() does to any string that forms a proper address.

And that's exactly what you are asking to have. Outside of VBA, an actual "performing kind" of macro or a UDF you write, it is the only way available in Excel. So if you don't know, or don't want to use, a VBA solution but rather need something that works in Formula World, then this is the only thing. Almost, to be honest, but certainly the easiest and most easily understood and built.

So, first you need your set-up. You have that, a place you want the output of it all, and a place where you have your formulas. Substiutute into the following as needed. What I do below is for data in cells A1:A5, three formulas I might like to use in B1:B3, a lookup value in C1, a cell reference for the formula I desire in C2, and a Named Range whose content I shall show now:

=EVALUATE(FORMULATEXT(INDIRECT(Sheet1!$C$2)))

Again, this will not work cell-side. It only works in the Named Range, nowhere else.

INDIRECT() takes the cell reference in C2 and passes it to FORMULATEXT() which procurs the formula in the cell specified in C2. That text string is passed through to the EVALUATE() function which treats it "like a real boy." Entering the formula:

=horse

(horse being the name I used for the Named Range)

in the cell you want the output gives you what you want.

An alternative is to build your formulas to be VERY relative. Probably have to build a set made relative for each cell in your work where you want their potential output. And extra care would have to be taken if they are to be "copied down" a column. So even though such a thing could conceivably be done, it would be a monstrous nightmare to set up, another to expand the usage of, and a further one to use down a column.

EVALUATE(), on the other hand, is simple and straightfoward. It fits precisely what you have already done. It is the last, simple and easy to use, last step you need in what you've done already.

But try EVALUATE() outside the Named Range functionality, try to use it inside a cell, and it will tell you there's no such thing. Stick with that and you'll not have success but rather will join the list of folks who get it wrong, then insist it just doesn't exist rather than figure out what went wrong. I feel you want success, so remember: use EVALUATE() in a Named Range, or not at all.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .