Forum Discussion
Nested IF function with a checkbox
Hello Excel Gurus,
I have a nested IF function that is based on comparing dates and whether a checkbox is selected. It's an 18 month outlook and if the checkbox is selected it should display "O/H" otherwise a "1." It functions perfectly if there is a 1 in all 18 blocks, but if it only has a 1 in some of the blocks, the "O/H" doesn't work properly. I'm trying to put a picture in to better clarify and show how the formula is setup.
Thanks for any assistance everyone provides.
Hello GregPen,
That clarification makes the intended logic clearer.
The checkbox should only change a cell that would otherwise contain 1. It should not override cells that the date logic would leave blank.
Try:
=IF($H36="","",IF($M36="",IF($AF36,"O/H",1),IF(N$6>=EDATE($M36,0),"",IF($AF36,"O/H",1))))
The key is that AF36 is checked only after the formula determines that the result for that month should be 1.
The logic is:
H36 is blank → blank
M36 is blank → 1, or O/H if AF36 is TRUE
N6 is on or after the PDD date → blank
Otherwise → 1, or O/H if AF36 is TRUE
This keeps the existing date cutoff while replacing only the 1s with O/H when the checkbox is selected.
It also keeps the AF36 test out of the EDATE calculation when M36 is blank, avoiding the #VALUE! problem.
16 Replies
- SelinaKnowIron Contributor
You can make the formula easier to follow by calculating the date once and then deciding whether that month is active. If the checkbox is linked to AF36 and returns TRUE/FALSE, try this Microsoft 365 formula:
=LET(pdd,IFERROR(--$M36,0),active,AND($H36<>"",OR(pdd=0,N$6<pdd)),IF(active,IF($AF36,"O/H",1),""))
Enter it in the first month cell and fill it across the 18-month range. The logic is: H36 must contain a value; a blank or invalid PDD means the month remains active; a real PDD blanks months on or after that date; and the checkbox changes only an active result from 1 to O/H. This avoids calling EDATE on a blank, a space, or text, which is what produces the #VALUE! result. Also confirm that AF36 contains the Boolean value TRUE or FALSE rather than the text “TRUE” or “FALSE.” Using LET here makes each condition visible and prevents the checkbox branch from being skipped inside a nested IF.
- m_tarlerSilver Contributor
Maybe we can group the conditions together better:
=IF(OR($H36="",N$6<$M36),"",IF($AF36,"O/H",1))I think this will work but not sure if I have all your conditions correct but based on Olufemi7 logic this should work becauce if M36 is blank that is treated as 0 and any date is > 0 so it will do the O/H or 1 check.
BTW I removed the EDATE($M36,0) because as far I can tell it doesn't do anything because a change of 0 months is still the same date, but if there is some other reason you have it there then feel free to put it back in.
EDIT: I notice you mentioned you were getting #VALUE! error which I suspect is due to the M36 NOT being blank ("") but a space (" ") or some other text instead, which will mess with a bunch of these formulas so in the formula below I added the edate back in with an IFERROR to catch that problem.
=IF(OR($H36="",N$6<IFERROR(EDATE($M36,0),0)),"",IF($AF36,"O/H",1)) - GregPenTin Contributor
I don't know why my responses won't post as replies to your posts, but here's what I found:
NathanReed Your formula kind of works as it changes the 1s to O/H as long as there is a date in the PDD block. If there is no date then all the cells get a VALUE error. I think the AND function is needed at the end to check that if there is a "1" AND the box is checked, then put "O/H".
Olufemi7 Your formula works better, but it changes all the cells to O/H in the row instead of just the cells with a 1. I think an AND function needs to be added as a final check in the formula but when I tried to come up with one I get a "too many arguments" error.
- NathanReedIron Contributor
It works when all 18 months contain 1 because, in that case, M36 is blank and the formula reaches IF($AF36,"O/H",1).
When fewer months contain 1, M36 contains a date, so Excel uses this branch instead:
IF(N$6>=EDATE($M36,0),"",1)
That branch never checks AF36, which is why the remaining 1s do not change to O/H.
Use this formula instead:
=IF($H36="","",IF(AND($M36<>"",N$6>=EDATE($M36,0)),"",IF($AF36,"O/H",1)))
- Enter it in the first month cell.
- Copy it across all 18 months.
- Select the checkbox linked to AF36.
Months beyond the date cutoff will remain blank. Any month that would normally display 1 will change to O/H when the checkbox is selected.
- GregPenTin Contributor
This formula kind of works as it changes the 1s to O/H as long as there is a date in the PDD block. If there is no date then all the cells get a VALUE error. I think the AND function is needed at the end to check that if there is a "1" AND the box is checked, then put "O/H".
- GregPenTin Contributor
This works if there is a date in the PDD cell, however, if there is no date, then I get a VALUE error. As I stated in the comment below, I think your AND statement is necessary but needs to be that if the the box is checked and there is a "1", then display O/H, otherwise leave it blank (maybe at the end as a final check?).
- GregPenTin Contributor
This formula didn't work and if it's in a row that doesn't have an OUTBOUND PDD, I get a value error.
- Olufemi7Steel Contributor
Hello GregPen,
The key is to decide which condition has priority.
If checking AF36 should always return O/H, regardless of whether M36 contains a date, then the checkbox test needs to be evaluated before the M36 test:
=IF($H36="","",IF($AF36,"O/H",IF($M36="",1,IF(N$6>=EDATE($M36,0),"",1))))
With this order, Excel evaluates:
H36 blank → blank
AF36 checked → O/H
M36 blank → 1
Otherwise → apply the date comparison
The original formula only checks AF36 when M36 is blank. Once M36 contains a date, Excel follows the other branch and AF36 is never evaluated.
If O/H is supposed to depend on both the checkbox and the date condition, then the checkbox needs to be incorporated into that date branch instead.
- GregPenTin Contributor
This formula works better, but it changes all the cells to O/H in the rom instead of just the cells with a 1. I think an AND function needs to be added as a final check in the formula but when I tried to come up with one I get a "too many arguments" error.
- GregPenTin Contributor
This kind of works in that it will display O/H across the entire row, however, I only need it to show O/H if there is a 1. I think I need an AND statement that says if there is a "1" and the checkbox is TRUE, then display O/H, otherwise leave it blank.
- GregPenTin Contributor
Thanks for the explanation. I figured it was an ordering problem but I get lost in the multiple IFs. This did partially fix the problem. It brings up O/H now but across the entire row and not just where the 1s populate. Would there be a better formula for this or are nested IFs the way to go?
- Olufemi7Steel Contributor
Hello GregPen,
That clarification makes the intended logic clearer.
The checkbox should only change a cell that would otherwise contain 1. It should not override cells that the date logic would leave blank.
Try:
=IF($H36="","",IF($M36="",IF($AF36,"O/H",1),IF(N$6>=EDATE($M36,0),"",IF($AF36,"O/H",1))))
The key is that AF36 is checked only after the formula determines that the result for that month should be 1.
The logic is:
H36 is blank → blank
M36 is blank → 1, or O/H if AF36 is TRUE
N6 is on or after the PDD date → blank
Otherwise → 1, or O/H if AF36 is TRUE
This keeps the existing date cutoff while replacing only the 1s with O/H when the checkbox is selected.
It also keeps the AF36 test out of the EDATE calculation when M36 is blank, avoiding the #VALUE! problem.
- GregPenTin Contributor
Here is what it looks like. I cut it down from 18 months to fit in a smaller picture but still show the problem.
Why didn't it change the two 1s to O/H.
- m_tarlerSilver Contributor
The formula is:
=IF($H36="","",IF($M36="",IF($AF36,"O/H",1),IF(N$6>=EDATE($M36,0),"",1)))But if $M36 is NOT "" then it will not do the IF($AF36...) because that is part of the TRUE and it will do the IF(N$6...) statement instead. I'm not sure what to reccommend because I'm not clear on what conditionals you are checking and corresponding outputs you are expecting in each case.
You might want to consider IFS(...) or other alternatives instead of the nested IF structure.
- GregPenTin Contributor
I just don't know why it works just fine if there are 1s across all 18 months, but not when there are less.