试图在设定的时间段内获取 Calendar Days/Workdays/Weeks 的数量

Trying to get the number of Calendar Days/Workdays/Weeks within a set period

我正在尝试创建一个脚本来获取设定时间段(上个月的 23 日和本月的 23 日)内的工作日、日历日和工作日的计数。

我有以下脚本,我尝试使用工作表函数但它不起作用,我得到

"Object variable or With block variable not set"

错误,是什么原因?

Sub DayCounts()
    Dim cYear As String
    Dim pMon As String
    Dim cpMon As String
    Dim start_date As String
    Dim end_date As String
    Dim mySh As Worksheet
    Dim wf As WorksheetFunction

    Set mySh = Sheets("prod_log_manual")

    cYear = Format(Date, yyyy) 'get year
    pMon = Format(Date - 1, mm) 'get previous month
    cMon = Format(Date, mm) 'get current month

    start_date = cYear & pMon & 24 '23th of previous month
    end_date = cYear & cMon & 23 '23rd of current month

    mySh.Range("P7") = wf.NetworkDays(start_date, end_date) 'get number of workdays in period
    mySh.Range("P8") = wf.Day(start_date, end_date) 'get number of calendar days in period
    mySh.Range("P9") = mySh.Range("P8").Value / 7 'get number of weeks within period
End Sub

错误

"Object variable or With block variable not set"

告诉你变量wf还没有设置,这意味着你声明了它

Dim wf As WorksheetFunction

但它仍然是空的,因为你没有用

初始化它
Set wf = Application.WorksheetFunction

这是你应该做的

Dim wf As WorksheetFunction
Set wf = Application.WorksheetFunction
mySh.Range("P7") = wf.NetworkDays(start_date, end_date) 'get number of workdays in period
mySh.Range("P8") = wf.Days(start_date, end_date) 'get number of calendar days in period

请注意,正如 Jeeped 在评论中指出的那样,它是 wf.Days 而不是 wf.Day

或使用WorksheetFunction.代替wf.或设置WorksheetFunction

mySh.Range("P7") = WorksheetFunction.NetworkDays(start_date, end_date) 'get number of workdays in period
mySh.Range("P8") = WorksheetFunction.Days(start_date, end_date) 'get number of calendar days in period

也许可以看看 Vityata 的回答,这将解决您在解决这个问题后 运行 遇到的下一个问题。

在模块顶部写上Option Explicit。因此,代码如下:

cYear = Format(Date, yyyy) 'get year
pMon = Format(Date - 1, mm) 'get previous month
cMon = Format(Date, mm) 'get current month

会开始告诉你出事了。例如,yyyymm 未定义为变量,这会让您认为它们应该是变量或作为字符串传递,如下所示:

cYear = Format(Date, "yyyy")

首先,根据 Vityata 的回复更正日期集合。

您可能希望使用实际日期,而不是字符串连接。

mySh.Range("P7") = wf.NetworkDays(dateserial(clng(cYear)+cbool(clng(pmon)=12), clng(pmon), 24), dateserial(clng(cYear), clng(cmon), 23)) 'get number of workdays in period

NetworkDays 包括在内。两者都应该使用 23 吗?上一个不应该是24还是现在应该是22?

您可以将 start_dateend_date 用作 Date,而不是 String,这将大大简化和缩短您的代码。

代码

Option Explicit

Sub DayCounts()

    Dim start_date As Date ' use Date variables
    Dim end_date As Date ' use Date variables
    Dim mySh As Worksheet
    Dim wf As WorksheetFunction

    Set mySh = Sheets("prod_log_manual")

    ' set the worksheetfunction object
    Set wf = WorksheetFunction

    end_date = DateSerial(Year(Date), Month(Date), 23) ' 23rd of current month
    start_date = DateAdd("m", -1, end_date) ' 23th of previous month

    mySh.Range("P7") = wf.NetworkDays(start_date, end_date) ' get number of workdays in period
    mySh.Range("P8") = wf.Days(start_date, end_date) ' get number of calendar days in period

    mySh.Range("P9") = mySh.Range("P8").Value / 7 'get number of weeks within period

    ' can also use DateDiff with "w", which is weeks as the interval
    mySh.Range("P9") = DateDiff("w", start_date, end_date)

End Sub