不确定为什么需要在 VBA 中使用它,您可以尝试以下方法。
在 Excel 中:
假设开始日期在 A1 中,结束日期在 A2 中,然后是 A3,
=(NETWORKINGDAYS(A1,A2))/5
现在这是从工作日的角度来看,因此每周有 5 天。如果您需要每周 7 天和正常的日子,
=WEEKNUM(A3)-WEEKNUM(A2)
分析工具包插件中的函数 WEEKNUM() 计算给定日期的正确周数(如果您在美国) 下面的用户定义函数将根据您计算机上的国家语言设置计算正确的周数。
如果您仍然需要使用 VBA,请尝试以下操作:(正如 Tim 指出的那样DateDiff
非常方便。)或者您甚至可以使用Evaluate to trigger WEEKNUM
.
Option Explicit
Function numWeeks(startDate As Date, endDate As Date)
numWeeks = DateDiff("ww", startDate, endDate)
End Function
在 WEEKNUM 上使用 Evaluate:
Function numWeeks(startDate As Range, endDate As Range)
Dim s As Integer, t As Integer
s = Application.Evaluate("=WEEKNUM(" & startDate.Address & ",1)")
t = Application.Evaluate("=WEEKNUM(" & endDate.Address & ",1)")
numWeeks = t - s
End Function