Forum Discussion
IF AND formula to check name and date then return YES, NO
Hello. I have a formula in Excel that checks if the name in cell A109 matches any of the names in the Name adjusted column of Table1. It also checks if the date in cell D105 matches the Leave Date column of Table1 for the matched name.
For example, A109 matches B61 in Name adjusted column of Table1 and D105 matches G61 in Leave Date column of Table1. If there is a match return YES otherwise NO.
The formula below is returning YES even when the corresponding rows in Table1 aren't a match. For example, B65 in Name adjusted column of Table1 is a match to A110 however the corresponding cell for the same row in Column G, G65, is not a match to D105.
Presumably because D105 is finding a match in other cells of Column G it is still returning YES. How do I adjust the below formula so that it only returns YES when the corresponding row for both Columns B and G of Table1 is a match. Example:
Return YES if,
Sheet 1: A109 = Name and D105 = 21/02/2024
Table1: B56 = Name and G56 = 21/02/2024
=IF(AND(IFERROR(INDEX(Table1[Name adjusted], MATCH(A109, Table1[Name adjusted], 0)), "") = A109, IFERROR(INDEX(Table1[Leave Date], MATCH(D105, Table1[Leave Date], 0)), "") = D105), "YES", "NO")
MAVOTRP To use the MATCH function with multiple criteria, try the following:
=IF(ISNUMBER(MATCH(1, (Table1[Name adjusted]=A109)*(Table1[Leave Date]=$D$105), 0)), "YES", "NO")Note: with older versions of Excel, you may need to press Ctrl+Shift+Enter when entering this formula.
For more information on how this works, please see: https://exceljet.net/formulas/index-and-match-with-multiple-criteria
Alternatively, you could also use the COUNTIFS function as follows:
=IF(COUNTIFS(Table1[Name adjusted], A109, Table1[Leave Date], $D$105), "YES", "NO")I hope that helps. Cheers!
7 Replies
- djclementsSilver Contributor
MAVOTRP To use the MATCH function with multiple criteria, try the following:
=IF(ISNUMBER(MATCH(1, (Table1[Name adjusted]=A109)*(Table1[Leave Date]=$D$105), 0)), "YES", "NO")Note: with older versions of Excel, you may need to press Ctrl+Shift+Enter when entering this formula.
For more information on how this works, please see: https://exceljet.net/formulas/index-and-match-with-multiple-criteria
Alternatively, you could also use the COUNTIFS function as follows:
=IF(COUNTIFS(Table1[Name adjusted], A109, Table1[Leave Date], $D$105), "YES", "NO")I hope that helps. Cheers!
- MAVOTRPCopper ContributorThis worked perfectly! Thank you so much!
- smylbugti222gmailcomIron Contributor
The formula you provided uses the IF and AND functions along with INDEX and MATCH to achieve the desired outcome, but there's a small adjustment needed to ensure it only returns "YES" when both the name and date match corresponding rows in Table1.
Here's the breakdown of your formula:
- IFERROR(INDEX(Table1[Name adjusted], MATCH(A109, Table1[Name adjusted], 0)), ""): This part searches for the name in cell A109 within the "Name adjusted" column of Table1 and returns the corresponding value if found, or an empty string ("") if not found. The IFERROR function handles potential errors if no match is found.
- = A109: This compares the retrieved value (or empty string) with the name in cell A109.
- Similar logic applies to the IFERROR statement for the date comparison.
The issue lies in the way the AND function works. It evaluates each condition independently. Even if the name matches in a different row (e.g., B65 in your example), the AND function might still evaluate to TRUE as long as the date matches somewhere else in the "Leave Date" column (e.g., G65).
Here's the corrected formula to address this:
Excel=IF(AND( INDEX(Table1[Name adjusted], MATCH(A109, Table1[Name adjusted], 0)) = A109, INDEX(Table1[Leave Date], MATCH(D105, Table1[Leave Date], 0)) = D105 ), "YES", "NO")
In this modified version, we removed the extra IFERROR statements and directly compared the results of the INDEX and MATCH functions with the corresponding values in Sheet 1 (A109 and D105). This ensures that both the name and date must match in the same row of Table1 for the formula to return "YES".
- MAVOTRPCopper ContributorHello. Thank you for your response. Unfortunately this still returns YES even if the name and date don't match in the same row. It seems this modified version is still picking up on D105 matching other cells in Column G and not necessarily the corresponding row that the name has matched in Column B. The response below however is correct and works so I have used that instead. Thanks again.
- djclementsSilver Contributor
smylbugti222gmailcom Please be advised:
By using A.I. tools to auto-generate answers without testing them for accuracy, you are potentially driving people away from this forum. A.I. very rarely produces Excel formulas that are 100% correct. More often than not, they contain false or misleading information, and in many cases, they are 100% wrong. Posting A.I. generated responses without having any personal knowledge of Excel formulas of your own is highly irresponsible. Please have the decency to at least test your findings before sharing them in the future.
- smylbugti222gmailcomIron Contributor
dear friend,
seeking help from others to solve your problem is a commendable act, if anyone who volunteers their expertise or time to assist you, regardless of the outcome, deserves appreciation and respect, disrespecting someone genuinely trying to help is unprofessional and counterproductive.
Remember, mutual respect fosters a collaborative environment. This message is not intended to hurt your feelings or escalate any jealousy. If you suspect this was generated by AI, feel free to use your own judgment or provide accurate data to collaborate effectively with other community members.