+ Reply to Thread
Results 1 to 7 of 7

Having a problem with my If Leap Year Function

  1. #1
    Registered User
    Join Date
    05-28-2013
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2010
    Posts
    3

    Having a problem with my If Leap Year Function

    So am creating a macro that inserts a column and part of what it needs to do is to put next years date afterwords. I at first simply had +365 to adjacent cell to get one year later. I then encountered that Excel accounted for leap year so I created this function. =IF(IF(OR(MOD(J3,400)=0,AND(MOD(J3,4)=0,MOD(J3,100)<>0)),"Leap Year","NOT a Leap Year")="Leap Year",J3+366,J3+365). (the previous year being located in J3).
    Basically it starts with a function determining whether or not it is leap year and then based on that result either adds 366 or 365. Problem is when I hit 2015 It adds 366 and goes over to 1/1/2016 instead of 12/31/2015. In visual right now I get 12/30/2020 , 12/31/2019 , 12/31/2018 , 12/31/2017 , 12/31/2016 , 1/1/2016 , 12/31/2014 , 12/31/2013 , 12/31/2012 , 12/31/2011 , 12/31/2010. Going from right to left.

    Any Ideas on what I'm doing wrong? I'm sure its something stupid.

  2. #2
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Having a problem with my If Leap Year Function

    Try the DateAdd function, it will automatically account for leap years.

    Dim MyDate As Date
    MyDate = DateAdd("yyyy", 1, Date)

    this will add 1 year to today's date (and auto account for leap years)

  3. #3
    Forum Expert Tony Valko's Avatar
    Join Date
    12-31-2011
    Location
    Pittsburgh
    MS-Off Ver
    2002, 2007:2013
    Posts
    18,890

    Re: Having a problem with my If Leap Year Function

    If J3 contains a date and you want to know if J3 is a leap year...

    =DAY(DATE(YEAR(J3),2,29))=29

    TRUE = leap year
    FALSE = not a leap year
    Biff
    Microsoft MVP Excel
    Keep It Simple Stupid

    Let's Go Pens. We Want The Cup.

  4. #4
    Registered User
    Join Date
    05-28-2013
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Having a problem with my If Leap Year Function

    Quote Originally Posted by Jonmo1 View Post
    Try the DateAdd function, it will automatically account for leap years.

    Dim MyDate As Date
    MyDate = DateAdd("yyyy", 1, Date)

    this will add 1 year to today's date (and auto account for leap years)
    Could you elaberate on this. I am a little confused on how to use this function in the way you described it.

  5. #5
    Forum Expert daddylonglegs's Avatar
    Join Date
    01-14-2006
    Location
    England
    MS-Off Ver
    2016
    Posts
    14,675

    Re: Having a problem with my If Leap Year Function

    You can use EDATE to add 12 months like this

    =EDATE(J3,12)

    That will account for leap years automatically
    Audere est facere

  6. #6
    Registered User
    Join Date
    05-28-2013
    Location
    Burlington, Vermont
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Having a problem with my If Leap Year Function

    Quote Originally Posted by daddylonglegs View Post
    You can use EDATE to add 12 months like this

    =EDATE(J3,12)

    That will account for leap years automatically
    Thanks that works great

  7. #7
    Forum Guru Jonmo1's Avatar
    Join Date
    03-08-2013
    Location
    Bryan, TX
    MS-Off Ver
    Excel 2010
    Posts
    9,763

    Re: Having a problem with my If Leap Year Function

    Quote Originally Posted by Cruse View Post
    Could you elaberate on this. I am a little confused on how to use this function in the way you described it.
    That's difficult without more information on what exactly you're doing.

    You said you're making a macro, so I posted a VBA solution instead of Cell formula.
    But didn't post any code to give an idea of what you're doing.
    So I can only give a simple example.

    That code creates a variable (Called MyDate) that is equal to the date 1 year from today's date.
    And it will automatically account for a leap year.

    You just need to then do whatever it was you were doing before, but use that variable instead.

    It's pretty much the same as daddylongleg's Edate formula.
    but it's based off of Today's date instead of J3

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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