+ Reply to Thread
Results 1 to 4 of 4

Datediff inside if statement

  1. #1
    Registered User
    Join Date
    01-03-2018
    Location
    Ohio
    MS-Off Ver
    2016
    Posts
    28

    Datediff inside if statement

    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")

  2. #2
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Datediff inside if statement

    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

  3. #3
    Valued Forum Contributor
    Join Date
    11-02-2016
    Location
    NY
    MS-Off Ver
    2010
    Posts
    459

    Re: Datediff inside if statement

    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

  4. #4
    Registered User
    Join Date
    01-03-2018
    Location
    Ohio
    MS-Off Ver
    2016
    Posts
    28

    Re: Datediff inside if statement

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with a S Q L statement inside VBA
    By VAer in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-30-2018, 08:32 AM
  2. [SOLVED] Can You Put An IF Statement Inside a REPLACE Statement?
    By nevi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2014, 07:34 AM
  3. If statement inside Solver?
    By anilaa88 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2013, 12:59 PM
  4. Using a VLOOKUP and IF statement inside an IF statement
    By Isis3 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-08-2011, 07:04 PM
  5. Using Next inside an IF statement
    By ChemistB in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-13-2010, 10:27 AM
  6. If, then inside a for, next statement?
    By EnergyEngineer in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 10-10-2007, 05:04 PM
  7. vlookup inside an if statement?
    By carlosgdlf in forum Excel General
    Replies: 3
    Last Post: 08-03-2005, 09:36 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1