Date plus one workday in macro

Posted on 2014-08-08
Last Modified: 2014-08-10
I have a macro that uses "Date" and "Now()" to evaluate date columns in the data. For example, if the date in the row is today, the macro colors that date cell blue. What I would like is to have the code change the cell to blue if the date is tomorrow (instead of today). There are enough instances where I thought rather than re-writing all the code, I could just change "Date" to "Date plus one workday" and "Now()" to either "Now() plus one workday" or use "Date plus one workday" instead.

Looks like "Date + 1" would certainly increment the date the code is working from but I can't find a good example on google of the syntax for a "Date + 1 networkday"

Also was wondering if I could just specify "date=date+1 networkday" at the begining so I wouldn't have to change all the instances of "Date".

Thanks in advance,

Question by:swjtx99
    LVL 20

    Accepted Solution

    Here is a function to calculate the next working day.
    It can be used in VBA calls, or as a function in a cell.
    When used as a cell function the code must be placed in a regular module, not a worksheet module.

    I have described different ways to set the holiday list, delete what you don't want, all does the same and just for test have Monday 8/11/2014 and Tuesday 8/12/2014 as holidays.
    See file.

    Function NextNetWorkDay(d As Date) As Date
        Application.Volatile    'When function is used as a function in a cell, means the function runs on every calculation
        Dim Holidays(), i As Integer
        'Defining the holidays list
        'A range on the sheet
            Holidays = Range("D2:D3")
        'A named range on the sheet
            Holidays = Range("Holidays")
            Holidays = [Holidays]
        'An array with the serial numbers of the dates
            Holidays = Array(41862, 41863)
        'or using dates as input, converted to serial numbers
            Holidays = Array(CLng(DateSerial(2014, 8, 11)), CLng(DateSerial(2014, 8, 12)))
        'or as dates, US format month/day/year, converted to serial numbers
            Holidays = Array(CLng(#8/11/2014#), CLng(#8/12/2014#))
            i = i + 1
            If WorksheetFunction.NetworkDays(d + i, d + i, Holidays) = 1 Then
                NextNetWorkDay = d + i
            End If
        Loop Until NextNetWorkDay > 0
    End Function

    Open in new window


    Author Closing Comment

    Thanks! I think I can use this for what I need.

    Thanks again for your help,


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Viewers will learn the basics about Excel 2013’s new Flash Fill feature.
    Video by: Zack
    Viewers will learn the basics of using Excel Tables, the benefits found with them, and some pitfalls.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now