【发布时间】:2013-02-07 15:09:57
【问题描述】:
我正在计算两个给定日期之间的工作时间(上午 8 点到晚上 8 点),不包括周末和公共假期,但我的代码语法不正确。
样本数据:
开始日期:17/06/2011 08:00:00 AM
结束日期:19/06/2011 08:00:00 PM
Sub SLA_Days_Resolved_F()
Dim x As Integer
' Set numrows = number of rows of data.
NumRows = Range("F2", Range("F2").End(xlDown)).Rows.Count
Dim total As Integer 'to count the total hours
Dim st As String 'start date cell
Dim en As String 'end date cell
Dim destCell As String
Dim d As Date ' for the loop
total = 0
' Establish "For" loop to loop "numrows" number of times.
For x = 2 To NumRows + 1
st = "G" & CStr(x) 'reference to the cells
en = "D" & CStr(x)
'loop from start date to end date
For d = Date(Range(st)) To Date(Range(en))
'check if the current date is found is a Public holiday in the range or if a weekend
If ((Vlookup(d,lookups!$o$3:$p$26,2,false))=1) or (weekend(d))Then
'minus 8 to remove hours before 8am.
total = (total + Hour(d) + minutes(d) / 60) - 8
End If
Next
Next
End Sub
【问题讨论】:
-
@David Zemmens:我缺少参考 st 和 en 变量的这行代码。 st = "G" & CStr(x) 和 en = "D" & CStr(x)。我已经在主帖上编辑了这个。
-
为什么不将
st和en标注为范围变量,并使用.offset(x,0)在For x...循环中重新定义它们。它将为您节省大量额外的编码。