Share via

Comparing dates in IF statement

Anonymous
2023-01-19T15:41:22+00:00

In an IF statement I want to check if date in A1 falls in the month immediately prior to the date in B1. I have tried various ways but none of them seem to work.

Can you help please.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

4 answers

Sort by: Most helpful
  1. Anonymous
    2023-01-22T10:53:35+00:00

    Thank you Hans. I nearly had this right, just overlooked to do double quotes on the third part of the IF statement.

    But without your help I would never have worked out the formula you suggested to my original post; I was not aware of EDATE function.

    Was this answer helpful?

    0 comments No comments
  2. HansV 462.6K Reputation points
    2023-01-20T14:35:43+00:00

    You have to double the quotes if you want to include a quoted string inside another quotes string, otherwise VBA thinks the string ends prematurely:

    ActiveCell.FormulaR1C1 = "=IF(TEXT(EDATE(E1,0),""yymm"")<TEXT(RC[-5],""yymm""),RC[-1],"""")"

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2023-01-20T13:17:02+00:00

    Thank you very much for your suggestions; they work just fine on the spreadsheet.

    But I have encountered problems when using it in vba. I get Compile Error: Expected End of Statement error when trying this:

    ActiveCell.FormulaR1C1 = "=IF(TEXT(EDATE(E1,0),"yymm")<TEXT(RC[-5],"yymm"),RC[-1],"")"

    Debug highlights "yymm" so I guess use of quote marks in the statement are incorrect. I tried ""yymm"" and removing them but still get errors.

    Would very much appreciate your help in getting this right.

    Thank you.

    Was this answer helpful?

    0 comments No comments
  4. HansV 462.6K Reputation points
    2023-01-19T15:52:43+00:00

    =TEXT(EDATE(B1,-1),"yymm")=TEXT(A1,"yymm")

    or

    =IF(TEXT(EDATE(B1,-1),"yymm")=TEXT(A1,"yymm"), "in prior month", "not in prior month")

    Was this answer helpful?

    0 comments No comments