0

I'm trying to loop a DATEDIF formula, using VBA, but I keep getting Run-time error '1004': Application-defined or object-defined error. I'm not sure why the code keeps kicking out. The formula itself works.

Sub DateandDaysFormula()


    With ThisWorkbook.Sheets("HDT Pivot Table")
        Dim lastRow As Long
        lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row

        .Range(.Range("L2"), .Range("L" & lastRow)).Formula = _
            "=DATEDIF(RC[-5],TODAY(),'d')"
        .Range(.Range("X2"), .Range("X" & lastRow)).Formula = _
            "=IFERROR(VLOOKUP(D2,DataDrop!A:C,2,FALSE),0)"
    End With

End Sub
1
  • Where is your loop? Commented Feb 11, 2019 at 20:23

1 Answer 1

0

If you use RC notation in a formula, you need to use FormulaR1C1, not just Formula.

Also, the d must be in double quotes, not single quotes. If you need to enter double quotes inside a VBA string, you need to double them. So, the problematic line should work with this change:

.Range(.Range("L2"), .Range("L" & lastRow)).FormulaR1C1 = _
            "=DATEDIF(RC[-5],TODAY(),""d"")"

It works in my tests.

Sign up to request clarification or add additional context in comments.

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.