尝试在设定的时间段内获取日历天数/工作日/周数

时间:2018-03-19 10:36:45

标签: excel vba excel-vba worksheet-function

我正在尝试创建一个脚本,该脚本在一个设定的时间段内(上个月的第23个月和当月的第23个月)获得营业日,日历天和工作日的计数。

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

  

“对象变量或未设置块变量”

错误,是什么给出了?

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

4 个答案:

答案 0 :(得分:3)

错误

  

“对象变量或未设置块变量”

告诉您变量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的答案,它将解决你在解决这个问题后会遇到的下一个问题。

答案 1 :(得分:3)

在模块顶部写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")

答案 2 :(得分:2)

您可以将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

答案 3 :(得分:1)

首先,根据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?