Excel VBA - Formula error in Calculating Neworking days

时间:2017-04-10 00:19:16

标签: excel vba excel-vba

I have a requirement where in, I am trying to calculate the Days Passed / Remaining for the deadline.

This is the VBA Code,

DaysVal = NetworkDays.INTL(E2, Today(), 1, DateTable([Holiday]))

I got the value of the date that I am checking through Programatically like Colm & i

DaysVal = NetworkDays.INTL(Colm & i, Date, 1, DateTable([Holiday]))

DateTable[Holiday] is just a table full of dates which are holidays placed in a different sheet.I will be writing this DaysVal as a new column in the same sheet where I am taking the Colm[E2]

Please kindly correct me where I am going wrong with the Formula.

Share your thoughts.

3 个答案:

答案 0 :(得分:2)

Here is an example using the Networkdays_Intl function with a structured reference for the holidays range:

Option Explicit
Sub foo()
    Dim DaysVal As Long
    Dim StartDate As Date

StartDate = #1/1/2017#

DaysVal = WorksheetFunction.NetworkDays_Intl(StartDate, Date, 1, [DateTable[Holidays]])

End Sub

The holidays range could also be represented as:

Range("DateTable[Holidays]")

答案 1 :(得分:1)

I would use something like this:

Function CalculateDaysDiff()

Dim A1DateValue As Date, DaysDiff As Integer
A1DateValue = Sheets(1).Cells(1, 1).Value

DaysDiff = Round(Now() - A1DateValue - 0.5)

CalculateDaysDiff = DaysDiff

End Function

I subtracted 0.5 because now() - someDate can return partial days (i.e. 1.8 days), and this rounded turns into 2, which probably is undesirable.

  • If 'CalculateDaysDiff' returns a positive value, then you expired the deadline by 'DaysDiff' days.
  • If 'CalculateDaysDiff' returns a negative value, then you have 'DaysDiff' days until the deadline.

答案 2 :(得分:0)

I believe this is the way to go, Still I am not able to get my company holidays as a holiday column, but got upto this.

StartDate = Range(Colm & i).Value
DaysVal = Application.NetworkDays_Intl(StartDate, Date, 1)

This works. Once, I get the holidays inside this. I will update my answer