1

i would like to use a multi-nested formula to determine if the dates in Column A fall in a specific month/year, using a formula such as =IF(AND(EXACT(TEXT(A8,"mmm"),"Jan",EXACT(TEXT(A8,"yyyy"),"2018"),"Y","N") However to make this formula more dynamic in VBA, i would like to replace the "Jan" with a custom month that i define via a pop-up input box, hence

Dim mth As String
FormulaR1C1= "=IF(AND(EXACT(TEXT(RC[-2],""mmm"")," & mth & ",EXACT(TEXT(A8,""YYYY""),""2018""),""Y"",""N"")"

When i do this, the formula fails to resolve properly because the "Jan" term appears without the quotes and so the two do not match: =IF(OR(EXACT(TEXT(A9,"mmm"),Jan)),"Y","N")

How can i modify the code to obtain "Jan" in my output please?

Thank you

edit: Dim mth As String mth = "Jan"

Set FormulaRange = Range("C7:C15") FormulaRange.FormulaR1C1 = "=IF(AND(EXACT(TEXT(RC[-2],""mmm""),""" & mth & """,EXACT(TEXT(A8,""YYYY""),""2018""),""Y"",""N"")"

1

2 Answers 2

1

To put double-quotes in a string in VBA you have to use double qoutes "", i.e. when you write """" it means that is that string containing one double-quote, hence your code should look like this:

Dim mth As String
FormulaR1C1 = "=IF(AND(EXACT(TEXT(RC[-2],""mmm""),""" & mth & """),EXACT(TEXT(A8,""YYYY""),""2018""),""Y"",""N"")"
Sign up to request clarification or add additional context in comments.

2 Comments

edit: i tried using the suggested line of code but got an application/object defined error
There's a closing bracket missing (in the OP too) from the first EXACT function.
0

If I understand right, you're trying to check if dates in column A are in a specific month, yes? Use

if MonthName(Month(c.value)) = userInput then
    ...
end if

for some Cell c and String userInput. You might also need to use CDate(c.value) depending on the format your dates are in.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.