Please write this excel formula for me: If AB2 = Open, then if true AA2 -1 if false AA2…
Private research annotation
Sign in to add a private research label or note.
- User
Please write this excel formula for me: If AB2 = Open, then if true AA2 -1 if false AA2 + 1, where AA2 is a date and -1 should give me the day before and + 1 gives me the day after
- Assistant
You can use the following Excel formula to achieve what you're looking for:
excel=IF(AB2="Open", AA2-1, AA2+1)Here’s how it works:
- If the value in cell AB2 is "Open", the formula will subtract 1 day from the date in AA2, returning the previous day.
- If the value in AB2 is not "Open", it will add 1 day to the date in AA2, returning the next day.
Make sure that the cell AA2 is formatted as a date for the output to reflect correctly as a date.
- User
I am getting a ~VALUE! error
- Assistant
The
#VALUE!error typically occurs in Excel when the formula is trying to perform an operation on incompatible data types. In your case, it's likely that the cell AA2 does not contain a valid date, which is necessary for the addition or subtraction operations to work.Here are some steps to troubleshoot and ensure the formula works properly:
-
Check the Date Format: Make sure that AA2 is formatted as a date. You can do this by right-clicking on the cell, selecting "Format Cells," and making sure it is set to a date format.
-
Ensure Valid Dates: Ensure that AA2 indeed contains a valid date value. If it's blank, text, or any other non-date value, it will throw a
#VALUE!error. -
Re-enter the Date: If you suspect that the date might be stored as text, try re-entering it manually to ensure it's recognized as a date.
Once you've ensured that AA2 contains a valid date, the original formula should work:
-
- User
How can I write this formula so that if AB2 = "" the the result will also be "": =IF(AB2="Open",AA2-1,AA2+1)
- Assistant
To modify the formula so that it returns an empty string (
"") if AB2 is empty, you can use a nestedIFstatement. Here is the updated formula:excel=IF(AB2="", "", IF(AB2="Open", AA2-1, AA2+1))Explanation:
- The first
IF(AB2="", "")checks if AB2 is empty. If it is, the formula returns an empty string. - If AB2 is not empty, it checks whether it equals "Open".
- If "Open", it returns
AA2-1(the day before). - If not "Open", it returns
AA2+1(the day after).
- If "Open", it returns
This way, if AB2 is empty, the result will also be an empty string as you desired.
- The first
- User
How can I write this formula so that if the date in AA2 is 31/12 of any year or 01/01 of any year then AA2-1 is AA2 and AA2+1 is AA2 but only for the dates 31/12 and 01/01
=IF(AB2="", "", IF(AB2="Open", AA2-1, AA2+1))
- Assistant
To modify your formula so that if the date in AA2 is December 31st or January 1st, it returns the same date for both the subtraction and addition cases, you can use the
ORfunction combined withDAY,MONTH, andYEARfunctions to check for those specific dates. Here's how you can write this:excel=IF(AB2="", "", IF(OR(AND(DAY(AA2)=31, MONTH(AA2)=12), AND(DAY(AA2)=1, MONTH(AA2)=1)), AA2, IF(AB2="Open", AA2-1, AA2+1)))Explanation:
- First Condition:
IF(AB2="", "", ...)checks if AB2 is empty, returning an empty string if true. - Second Condition:
OR(AND(DAY(AA2)=31, MONTH(AA2)=12), AND(DAY(AA2)=1, MONTH(AA2)=1))checks if AA2 is either December 31st or January 1st:DAY(AA2)=31, MONTH(AA2)=12checks for December 31st.DAY(AA2)=1, MONTH(AA2)=1checks for January 1st.
- If AA2 is either of these dates, it returns .
- First Condition: