我必须找到两个日期之间的工作日数,这应该排除周末和国定假日。 我在vba中使用NETWORKDAYS功能,这不包括周末,但我想排除一些国家假日。 如何在国家法定假日使用此功能NETWORKDAYS(startDate,endDate,[holidays])。它说它接受[假日]作为清单。我把所有的国定假日放在一个阵列中。如何通过VBA将此功能用于此功能? 请找到代码段。
Public Function dataFromInputSheetHolidayDates() As Variant
Dim holidayDates As Integer
Dim holidaydatesArray(20) As Variant
holidayDates = Sheets("InputSheet").Cells(Rows.Count, "D").End(xlUp).Row
For countDate = 0 To holidayDates - 1
holidaydatesArray(countDate) = Format(Sheets("InputSheet").Cells(countDate + 2, "D").Value, "DD-MMM-YY")
Next countDate
dataFromInputSheetHolidayDates = holidaydatesArray
End Function
holidayList = dataFromInputSheetHolidayDates()
Sheets("Estimation").Range("Z" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",Y" & taskcounter &","& holidayList & ")"
Sheets("Estimation").Range("AB" & taskcounter).Formula = "=NETWORKDAYS(X" &
taskcounter & ",AA" & taskcounter &"," & holidayList & ")"
答案 0 :(得分:0)
更改代码中的这两行:
Dim holidayDates As Long
holidaydatesArray(countDate) = Sheets("InputSheet").Cells(countDate + 2, "D")
在VBA
中,整数最多为32767
,这对日期来说还不够。此外,holidaydatesArray
应该具有数值而不是某种文本格式。
与此问题非常类似的问题 - workday holiday argument doesn't accept array
如果您尝试通过VBA创建灵活的公式,假期在不同的工作表上,请尝试此解决方案:
Public Sub TestMe()
Dim holidayLists As Range
Set holidayLists = Worksheets(2).Range("D1:D10")
With Worksheets(1)
.Range("A1").Formula = "=NETWORKDAYS(B1, C1," & holidayLists.Parent.Name & "!" _
& holidayLists.Address & ")"
End With
End Sub
holidayLists.Parent.Name & "!" & holidayLists.Address
会正确引用工作表的假期名称。