I need a datediff statement inside an if statement in a VBA module. I am not sure of the syntax. This gives me a syntax error.
If (Month(A2) = 1 and Month(B2) = 1) Then DATEDIFF(A2,B2,"d")
I need a datediff statement inside an if statement in a VBA module. I am not sure of the syntax. This gives me a syntax error.
If (Month(A2) = 1 and Month(B2) = 1) Then DATEDIFF(A2,B2,"d")
Hello
What is your expected result .. you need to get the difference by years or months or days ..
And where would you like to display the result ?
< ----- Please click the little star * next to add reputation if my post helps you
Visit Forum : From Here
I did this without VBA.
Cell a1 = Sdate Cell b1 = Edate cell c1 = datedif
cell a2 = 1/5/2018 Cell b2 = 1/20/2018 ANSWER = 15
cell a3 = 2/1/2018 Cell b3 = 1/30/2018 ANSWER = "DiffMonth" [ the start date is greater than the end date ~ results in an error]
In C2 =IF(MONTH(A2)=MONTH(B2),DATEDIF(A2,B2,"d"),"DiffMonth") ANSWER = 15
A better question might be this: (it works fine in Excel)
How do I put this statement in a VBA module to appear in Column F1? Difference by days.
=IF(MONTH(A2)=1,IF(MONTH(B2)=1,DATEDIFF(A2,B2,"d")))
Because I need to have this in there as well:
If targetWorkbook.Worksheets("Sheet1").Range("A2")
Last edited by ngenova; 09-27-2018 at 01:23 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks