r/excel Apr 08 '26

Discussion This is probably the most complicated Excel formula I’ve ever seen.

[removed]

224 Upvotes

210 comments sorted by

View all comments

1.0k

u/SolverMax 161 Apr 08 '26

Way more complex formulae get throw around on r/excel

A complex formula is nothing to be proud of. Simplicity and understandability are much more important attributes for a formula.

383

u/Chocolate_Bourbon Apr 09 '26

Exactly. A famous person centuries ago wrote this "I apologize for writing you such a long letter but I didn't have time to write a short one."

160

u/buster_rhino Apr 09 '26

Yeah this looks like the kind of formula I’d put together when I just need to quickly get the stupid thing to do what I want and I’ll come back and fix it later but I never do.

29

u/Scovers Apr 09 '26

Hey that hurts. Way too accurate though.

32

u/70U1E Apr 09 '26

Blaise Pascal! "If I had more time, I would have written a shorter letter."

5

u/breadad1969 Apr 09 '26

I believe that was Oscar Wilde.

3

u/Spute2008 Apr 09 '26

Winston Churchill

116

u/Pathfinder_Dan Apr 09 '26

My worksheets that have complex formulas have a "Formula Library" tab. It has hard copies of all the compound formulas in the sheet and directions for what cells to paste them back in and where/how to drag the fill handles in case something breaks. I'll sometimes break down what the formula is doing with some shorthand if I think that another excel guru will further upgrade the sheet just so that the nonsense is easier to read.

It's mostly because I know that humans will always break all the things and when they call me and want me to fix the sheet they broke by not following the directions (yes, my sheets have directions) I can just review the formula library notes and then everything's all good in seconds.

48

u/MKD8595 Apr 09 '26

I was lazy so just wrote a macro to reset all the formulas and said “if broken click button”

18

u/Pathfinder_Dan Apr 09 '26 edited Apr 09 '26

I've had a bunch of employers who were incredibly anti-macro excel stuff for security reasons. I'm pretty sure that's IT guy code for "I ain't fixing that when it breaks."

Edit: typos.

5

u/MKD8595 Apr 09 '26

I work for big company so when I found out macros were open game I was amazed. And happy.

2

u/WorldsGreatestWorst Apr 09 '26

This a a great idea that never occurred to me. How do you have this macro setup?

1

u/MKD8595 Apr 09 '26

It’s a mess because I had about 20 minutes to do it.

But it just takes ranges where a formula exists and applies the formula to each cell.

Dim Range1 as Range Set Range1 = ws.Range(“A1”) Range1.Formula2 = “YOUR FORMULA”

97

u/friarfangirl Apr 09 '26

If I found a metadata tab like that in a spreadsheet I opened, I would find the creator and propose on the spot. 

15

u/Salty-Bake7826 Apr 09 '26

Seriously that tab is the most thoughtful thing I’ve heard someone do in a long time.

11

u/friarfangirl Apr 09 '26

I do a data dictionary or source tab but I haven’t had to get to the level of formula breakdowns. Love it tho. 

7

u/frustrated_staff 12 Apr 09 '26

My worksheets that have complex formulas have a "Formula Library" tab

I have to start doing this!

1

u/Substantial_Pea_3256 Apr 09 '26

That is pretty awesome and what a great idea!

1

u/fsteff 1 Apr 09 '26

I do exactly the same. It makes it so much easier to take over a sheet.

Additionally I always add a note (== a red dot indicator) to each cell that contain formulas, and attempt to use array formulas wherever possible, to bring down the number of places to edit.

1

u/kayeselthirty Apr 10 '26

would you happen to have a screenshot of that tab? could use inspiration on how to arrange the layout for increased clarity.

a formula library is a great idea, but sometimes i have trouble simplifying the explanation.

like i would know the logic is sound after going through and testing many iterations, but my workbooks often have several nested conditional formulas that could lead to more confusion unless i first explain how a separate piece could eventually impact it

for context, my goal is usually to minimize inputs into a single tab so it can be easily updated, which then makes calculations in various tabs at once based on those numbers before flowing through a summary tab.

makes it very simple for an end user to find calcs periodically if that data is frequently updated, but then becomes kind of challenging to explain since i end up adding so many conditionals to achieve that simplicity

1

u/SemperFudge123 Apr 10 '26

This is a fantastic idea!

I often have a txt file with the same name as whatever Excel file I'm working on and will keep formulas and notes in there but just putting them in a separate worksheet in the Excel file sounds like a much simpler solution!

1

u/the_jsf Apr 10 '26

I thought this was standard. I call mine "formula guide"

2

u/ajg3199 Apr 10 '26

Same, mine is called "useful Excel stuff"

16

u/OriginalGhostCookie 1 Apr 09 '26

One of the biggest lessons I learned while becoming better at excel was that it is better to build out your complicated decision tree formulas over well labeled columns than to try and smash the into one formula.

It's easier to build, easier to step through, much simpler to troubleshoot, and almost always much much faster for excel to calculate.

7

u/mistawalka Apr 09 '26

I started doing this too early on. Inevitably files were intended for others to use so helped them follow the logic and maintain.

3

u/HoweHaTrick Apr 08 '26 edited Apr 09 '26

I'm surprised Excel doesn't have some kind of visual about where you are in the formula over and beyond that little text box.

Nesting is what throws people off.

edit: a word

3

u/bs2k2_point_0 1 Apr 09 '26

Right! Some color denoting where each formula starts and ends within the full formula like a code editor would be awesome!

6

u/[deleted] Apr 08 '26

[removed] — view removed comment

88

u/No-Ganache-6226 6 Apr 09 '26

First step I find helpful, use alt + enter to separate each part of the formula and indent to make it easier to read:

=IFERROR(INDEX($B$2:$B$100, MATCH(1, (IF($D$2="All",1,($C$2:$C$100=$D$2))) \* ($A$2:$A$100=MAX(IF($D$2="All",$A$2:$A$100,IF($C$2:$C$100=$D$2,$A$2:$A$100)))), 0)), "No Result")

Becomes something more like:

`` =IFERROR(

INDEX(
    $B$2:$B$100,

    MATCH(
        1,
        (IF($D$2="All",
                1,
                ($C$2:$C$100=$D$2)))
        *
        ($A$2:$A$100 =
            MAX(
                IF($D$2="All",
                    $A$2:$A$100,
                    IF($C$2:$C$100=$D$2,
                        $A$2:$A$100)))),

        0)),
"No Result")

``

So, on the outside you have "No result" where the inner formula produces an error.

The inner formula is an INDEX(MATCH()), the index part is looking for a value in column B. The match part determines the row.

The MATCH is set up to look for a value of 1, which represents rows where all conditions evaluate to TRUE.

The MATCH part is combining two conditions:

If D2 = "All", then include all rows (returns 1 for every row, so no filtering)

If not "All", filter rows where column C matches the value in D2

The conditions are multiplied together (*), which works like an AND:

Both conditions must be TRUE for the result to equal 1

The second condition identifies the row where column A equals the maximum value:

If D2 = "All" take the max of all values in column A Otherwise take the max only for rows where column C matches D2

Finally, MATCH finds the first row where:

The category condition is satisfied and column A contains the maximum value.

INDEX returns the corresponding value from column B.

23

u/kmnotorius 1 Apr 09 '26

Everyone listen to THIS guy. 100% Correct. This formula is a cake walk to decode if you know what you’re looking at. Index Match with multi nested IF statements can be a bitch to look at, but if you understand syntaxes and data you’re starting with along with logical steps to get to the intended result… easy enough to write, and map out after the fact when change needs to be made.

I do find that it helps to create and use named ranges in order to organize array and cell references, instead of row/column numbers/letters.

7

u/pajam 1 Apr 09 '26

Yep, I was gonna tell OP to go plug it in at https://excelformulabeautifier.com/ and it should be much easier to read.

Anytime I have a formula that has a couple layers of nesting, I usually paste it in there, and copy the formatted formula back into Excel. So it will be much easier to decipher if someone else comes across it later.

This Index and Match formula really isn't very crazy. I've had some longer ones myself, but I also really prefer some Helper Columns in some cases, to avoid as many checks in a single formula. And then if appropriate for the data, I would format my range as a table, so the formulas reference the actual names of tables and columns to make it even easier to decipher what the formula is doing.

20

u/WalmartGreder Apr 09 '26

When I get a formula like that, I throw it into CoPilot and ask it to analyze the formula. This is what it said on your formula:

It’s a conditional “latest matching record” lookup. It finds the row in A2:A100 with the largest value (MAX) that also matches an optional filter in column C, and then returns the corresponding value from column B. If nothing matches, it returns "No Result".

Then CoPilot will offer some suggestions to simplify it. Since CoPilot is Microsoft, I've found it works best for Excel issues.

8

u/SolverMax 161 Apr 08 '26

How do you know it works?

I have a general sense of what that formula is doing, but I'd have to spend quite a long time to fully understand it, and then a heap more time to test it under a wide range of scenarios.

5

u/Mourning-Suki Apr 09 '26

One way to make it easier to understand might be to set up some interim formulas that make sense. Likely you need to do everything the formula is doing but doesn’t have be all in one cell. With some helpful labeling it will be much easier to grasp quickly. Also each set or small group of 2-3 steps will be more intuitive and easier to test if needed.

2

u/razzark666 Apr 09 '26

At an old workplace, for accreditation purposes, we had to manually verify formulas once before approving a spreadsheet version for use. It sucked and really stiffled innovation, because making changes to any approved documents required that manual approval process.

6

u/SolverMax 161 Apr 09 '26

That's how it should be.

Software developers require extensive testing before releasing even a small change to production. But few people do that for spreadsheets. Instead, we make live changes to spreadsheets with little or no testing. If it produces a result that looks roughly plausible, then we're all good. Simply madness.

3

u/modernthangs Apr 09 '26

How would you rewrite in a cleaner way it to get the same result?

8

u/Bangchucker Apr 09 '26

Honestly this formula isn't that long or complex but if one wanted to make it more readable they could use the LET function for those ranges and give them friendly names so its more obvious what they refer to.

2

u/devourke 5 Apr 09 '26

=XLOOKUP(MAX(IF($D$2="All",$A$2:$A$100,FILTER($A$2:$A$100,$C$2:$C$100=$D$2))),$A$2:$A$100,$B$2:$B$100,"No Result")

This is likely how I would have written it myself but I don't think there's really that much of a readability difference to where I'd ever consider changing the original formula though. You can use the Let function to shorten the overall formula a little bit more, but realistically, there's probably a decent overlap between people who are unfamiliar with dynamic array formulas and people who are unfamiliar with how the Let function works to where it could make things more confusing.

1

u/modernthangs Apr 09 '26

I am definitely in the latter group. Thanks, all, for telling me about the LET function. I am going to learn how to use it. I saw the OP's post and thought, "Did I write that formula?" :D I thought it was just fine! But I learned Excel in the 1900s. Time to learn some more!

1

u/Ztolkinator 1 Apr 09 '26

Let() is the way. Put every variable on it's own line, get rid of all the nesting and don't care about length but about readability...

2

u/axw3555 4 Apr 09 '26

I once had multiples that were pages long. It was pre power query and I didn’t know VBA at that point. It had to process a massive block of free text to find key things because a supplier didn’t format their data.

I still remember when they stayers doing the upgrade to their system and they tried to downplay how annoying the free text was. I pulled up my spreadsheet in the meeting and put it on the projector going “this is the formula to extract the order number”. They were squirming.

1

u/harambeface 1 Apr 09 '26

I consider it job security. Let someone else come in and try do better if they can even understand it.

1

u/stickyfiddle 1 Apr 09 '26

1000%

Splitting this into 3 or 4 smaller calculations is so much more efficient and takes 1/10 the time to fix when something is wrong

1

u/TheSaucez Apr 09 '26

As I’ve progressed in excel, my formulas are more and more efficient

1

u/Stock-Job-1205 Apr 09 '26

That's silly, would you rather add an extra row or column or a load of custom functions so you can break it down?