VBA获取两个日期之间的工作日

时间:2019-07-01 10:07:55

标签: excel vba

所以,我有一个代码正在打印“ mon-sun”的日子。每个单元格一天。这就是我想要的。但是目前,它是从K开始到Q结束,我希望它从正确的单元格中的开始日期开始。

因此,如果开始日期为2019-07-03,则为星期三。它应以“ M”而不是“ K”列开头。另外,在星期日停止。因此,如果开始日期和结束日期超过一周,则应该在星期日之后自动停止。

Dim FirstDate As Date
Dim LastDate As Date
Dim NextDate As Date
Dim DateOffset As Range
Dim DateIter As Date

FirstDate = Source.Range("J" & cell.Row).Value
LastDate = Source.Range("K" & cell.Row).Value
Set DateOffset = Target.Range("K" & LastRow + 1)

For DateIter = FirstDate To LastDate
    DateOffset.Value = DateIter
    Set DateOffset = DateOffset.Offset(0, 1)
Next DateIter

              End With

            End If

        Next cell

enter image description here

编辑:

尝试此方法,但有时却不起作用。

FirstDate = Source.Range("J" & cell.Row).Value
LastDate = Source.Range("K" & cell.Row).Value
Set DateOffset = Target.Range("K" & LastRow + 1)



For DateIter = FirstDate To LastDate
'    DateOffset.Value = DateIter
'    Set DateOffset = DateOffset.Offset(0, 1)

If Weekday(DateIter, vbMonday) = 2 Then

Call anmalan_demo.Details(.Range("K" & LastRow + 1), "Må")


End If

If Weekday(DateIter, vbTuesday) = 3 Then

Call anmalan_demo.Details(.Range("L" & LastRow + 1), "Ti")

End If

If Weekday(DateIter, vbWednesday) = 4 Then

Call anmalan_demo.Details(.Range("M" & LastRow + 1), "On")

End If

If Weekday(DateIter, vbThursday) = 5 Then

Call anmalan_demo.Details(.Range("N" & LastRow + 1), "Tor")

End If

If Weekday(DateIter, vbFriday) = 6 Then

Call anmalan_demo.Details(.Range("O" & LastRow + 1), "Fre")

End If

If Weekday(DateIter, vbSaturday) = 7 Then

Call anmalan_demo.Details(.Range("P" & LastRow + 1), "Lö")

End If

If Weekday(DateIter, vbSunday) = 1 Then

Call anmalan_demo.Details(.Range("Q" & LastRow + 1), "Sö")

End If


Next DateIter

              End With

            End If

        Next cell

1 个答案:

答案 0 :(得分:0)

这可以通过NetworkDays()NetworkDays.Intl()工作表函数来完成,如this URLthis URL中所述。

如果要在VBA中使用它,则可能需要执行以下操作:

Application.WorksheetFunction.NetworkDays_Intl(...)