+ Reply to Thread
Results 1 to 5 of 5

Function to return a date of the nearest same day

  1. #1
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Function to return a date of the nearest same day

    I need to create a function (in VBA) that will return me a future date that is on the same day as the original date. So in essence I want to predict a future date as close to the same day as the original date as possible while still a workday e.g.

    Today is 13 March 2014 and it is a Thursday so function could look something like this
    Please Login or Register  to view this content.
    Take todays date and add 30 days to it which would take us to 12 April 2014 but this is a Saturday so the nearest Thursday to this date would be 10 April 2014 so the return date should be 10 April 2014. But this is only the first part...

    The third parameter is something like +1/-1. If the date calculated falls on a public holiday the date returned should be the the previous workday (for a -1) or the next workday (for a +1). This part will have to use the Workday function of Excel because it can give the previous workday or the next workday and it can take a range of dates (public holidays) that will be excluded.

    Hope this makes sense. Any pointers in the right direction?

  2. #2
    Forum Expert Kenneth Hobson's Avatar
    Join Date
    02-05-2007
    Location
    Tecumseh, OK
    MS-Off Ver
    Office 365, Win10Home
    Posts
    2,573

    Re: Function to return a date of the nearest same day

    Did you want to add 30 days or 30 working days or one month or other?

    =WorkDay or =WorkDay.Intl should get you close. The code with then need to increment or decrement the days to find the closest Thursday. There is a problem though using one of the built-in functions. That is the case where you want to add or subtract one day for a holiday. I suspect that it gets the next day after holidays.

  3. #3
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: Function to return a date of the nearest same day

    Here is formula solution for first part:

    Starting date in A1, number of days in B1 in and the formula in C1:


    =$A$1+B1-MOD(B1+3;7)+3

    now second part:

    in D1 your +/-1

    in G1:G10 list of bank holidays.

    in F1 final result:
    =WORKDAY(C1-D1,D1,G1:G10)

    Of course you can nest it all into one formula, or - if you wish so - prepare VBA function, using the above algorithm
    Best Regards,

    Kaper

  4. #4
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Function to return a date of the nearest same day

    Kenneth: The date I specifically want returned is; What is the date of the closest workday of the same day (say a Friday) in one year (+365 days) but if it's a public holiday what is the workday before (-1) or the workday after (+1) the public holiday. The number of days to add could be anything but I'm wanting 365 days at this stage. The day could be any weekday (working days).

    Kaper: I'm going to look into your suggestions.

  5. #5
    Forum Contributor
    Join Date
    12-06-2012
    Location
    South Africa
    MS-Off Ver
    Excel 2010
    Posts
    201

    Re: Function to return a date of the nearest same day

    Got it working. Thanks Kaper, your suggestion is super.

+ 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. Replies: 5
    Last Post: 01-13-2014, 01:01 AM
  2. [SOLVED] Return nearest value to specified time within + or - 1 min.
    By PeanutPete in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-04-2013, 06:47 AM
  3. Replies: 9
    Last Post: 12-19-2012, 01:06 AM
  4. [SOLVED] Compare strings for match in separate worksheet and return nearest future date
    By kungfood in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-05-2012, 05:19 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