HOW to add 15 working day to 05/02/2020...

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

HOW to add 15 working day to 05/02/2020...

Post by sal21 »

Debug.Print DateAdd("D", 15, myDate)

return 20/02/2020

but i need to calculate only a working day, without all holyday days

User avatar
HansV
Administrator
Posts: 78572
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to add 15 working day to 05/02/2020...

Post by HansV »

Excel has a WORKDAY function, but I assume that you want to do this in VB6.
Do you have an array with public holidays, or a holidays table in a database, or …?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: HOW to add 15 working day to 05/02/2020...

Post by sal21 »

HansV wrote:Excel has a WORKDAY function, but I assume that you want to do this in VB6.
Do you have an array with public holidays, or a holidays table in a database, or …?
In this time i need only to add days without SABATO and DOMENICA

User avatar
HansV
Administrator
Posts: 78572
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to add 15 working day to 05/02/2020...

Post by HansV »

Here is a function:

Code: Select all

Function Workday(StartDate As Date, Days As Long) As Date
    Dim N As Long
    Dim C As Long
    Dim I As Long
    Dim D As Long

    I = Sgn(Days)
    D = Abs(Days)
    Do Until C = D
        N = N + I
        If Weekday(StartDate + N, vbMonday) < 6 Then
            C = C + 1
        End If
    Loop

    Workday = StartDate + N
End Function
Example of use:

Code: Select all

Debug.Print Workday(MyDate, 15)
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4364
Joined: 26 Apr 2010, 17:36

Re: HOW to add 15 working day to 05/02/2020...

Post by sal21 »

HansV wrote:Here is a function:

Code: Select all

Function Workday(StartDate As Date, Days As Long) As Date
    Dim N As Long
    Dim C As Long
    Dim I As Long
    Dim D As Long

    I = Sgn(Days)
    D = Abs(Days)
    Do Until C = D
        N = N + I
        If Weekday(StartDate + N, vbMonday) < 6 Then
            C = C + 1
        End If
    Loop

    Workday = StartDate + N
End Function
Example of use:

Code: Select all

Debug.Print Workday(MyDate, 15)
WORK Bro!

But possibe, during this loop, how to debug.print the list of days in this format DD/MM/YYYY?

example:
N=15

05/02/2020
06/02/2020
07/02/2020
10/02/2020
11/02/2020
...
24/02/2020

User avatar
HansV
Administrator
Posts: 78572
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: HOW to add 15 working day to 05/02/2020...

Post by HansV »

Code: Select all

Function Workday(StartDate As Date, Days As Long) As Date
    Dim N As Long
    Dim C As Long
    Dim I As Long
    Dim D As Long

    I = Sgn(Days)
    D = Abs(Days)
    Do Until C = D
        N = N + I
        If Weekday(StartDate + N, vbMonday) < 6 Then
            Debug.Print Format(StartDate + N, "dd/mm/yyyy")
            C = C + 1
        End If
    Loop

    Workday = StartDate + N
End Function
Best wishes,
Hans