VBA - 营业日-1

时间:2015-11-12 15:03:12

标签: excel-vba vba excel

我有以下代码给我B栏中的营业日。但是,我需要A栏才能有前一天的日期。例如:A应该有B-1。如果它是一个周末,它应该有星期五的日期。请协助。

With Worksheets("Majors")
.Range("B3").ClearContents
Select Case Month(Now)
Case 2: mMax = IIf(Year(Now) Mod 4 = 0, 29, 28)
Case 4, 6, 9, 11: mMax = 30
Case Else: mMax = 31
End Select
B = 2
For mDay = 1 To 31
wNam = WeekdayName(Weekday(DateSerial(Year(Now), Month(Now), mDay)), True,  1)
If Not wNam = "Sat" And Not wNam = "Sun" Then
    B = B + 1
    Cells(B, 2) = (DateSerial(Year(Now), Month(Now), mDay))
End If
If mDay = mMax Then Exit For
Next mDay

End With

1 个答案:

答案 0 :(得分:1)

这给出了A1之前的工作日:

mDate = CDate(Range("A1"))
AnteWorkDay = DateAdd("d", -1, mDate)
Do While Weekday(AnteWorkDay) = vbSunday Or Weekday(AnteWorkDay) = vbSaturday
    AnteWorkDay = DateAdd("d", -1, AnteWorkDay)
Loop
MsgBox AnteWorkDay