gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 21:09:05 29 4
gpt4 key购买 nike

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

我有以下脚本,我尝试使用 Worksheet Functions 但它不起作用,我明白了

"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

最佳答案

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

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

会开始告诉你有什么问题。例如, yyyy , mm未定义为变量,这会让您认为它们应该是变量或作为字符串传递,如下所示:
cYear = Format(Date, "yyyy")

关于vba - 尝试在设定的时间段内获取日历天数/工作日/周数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49360853/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com