[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 319
  • Last Modified:

Date plus one workday in macro

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,

swjtx99
0
swjtx99
Asked:
swjtx99
1 Solution
 
Ejgil HedegaardCommented:
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")
    'or
        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#))
    
    Do
        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

Next-working-day.xlsm
0
 
swjtx99Author Commented:
Thanks! I think I can use this for what I need.

Thanks again for your help,

swjtx99
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now