2015-11-12 65 views
0

我有以下代碼給我在B列的營業日。但是,我需要列A有前一天的日期。例如:A應該有B-1。如果是週末,它應該有星期五的日期。請協助。VBA - 營業日-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 
+0

作爲一個側面說明,你可以只用一個簡單的線條重構你的'選擇Case'聲明:'MMAX =日(申請.WorksheetFunction.EoMonth(Now(),0))'。你也可以用'mDay = 1到mMax'來重構你的循環,然後刪除'If mDay = mMax Then Exit For' –

回答

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