VBA进入营业日?

时间:2015-11-10 01:33:08

标签: excel vba excel-vba

我正在尝试为模板编写代码。每个月我都希望代码进入工作日。但是,当我尝试代码时,它会更新工作日,但不会更新我想要的电子表格。它将更新person excel文件的日期。请指教。

Sub MG09Nov02
Dim mDay        As Integer
Dim Col         As Integer
Dim Dt          As Integer
Dim mMax        As Integer
Dim wNam        As String
Dim c           As Long
Range("B:C").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
c = 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
    c = c + 1
    Cells(c, 2) = WeekdayName(Weekday(DateSerial(Year(Now), Month(Now), 
mDay)), True, 1)
    Cells(c, 3) = (DateSerial(Year(Now), Month(Now), mDay))
End If
If mDay = mMax Then Exit For
Next mDay
End Sub

1 个答案:

答案 0 :(得分:1)

如上所述,您实际上并未披露代码用于列出营业日的工作表名称。

Sub MG09Nov02()
    Dim mDay        As Integer
    Dim mMax        As Integer
    Dim c           As Long

    With Worksheets("BDAYS")  '<~~ set this worksheet reference properly!
        .Range("B:C").ClearContents

        mMax = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
        c = 2

        For mDay = 1 To mMax
            If Weekday(DateSerial(Year(Now), Month(Now), mDay), vbMonday) < 6 Then
                c = c + 1
                .Cells(c, 3) = DateSerial(Year(Now), Month(Now), mDay)
                .Cells(c, 2) = Format(.Cells(c, 3), "ddd")
            End If
        Next mDay
    End With

End Sub

您可能想要探索的另一种方法是将实际日期放在B列中,格式为 ddd 以生成 Mon,Tue,Wed等。同时仍保留其完整日期值。