首页 > 解决方案 > Excel VBA - 从自动时间表打印中删除周末

问题描述

我稍微修改了一些我在网上找到的代码。

目的:

单击“打印日期”按钮,然后输入开始日期,并让 Excel 自动生成/打印一个月的时间表(比以前的电子表格要打印 6 周的页面要好得多,而且您必须手动编辑每个日期)。

问题:

代码:

Sub PrintSheet()
    Dim s As String
    Dim d As Date
    s = InputBox(Prompt:="Please enter the start date")
    If Not IsDate(s) Then
        MsgBox "Incorrect date", vbExclamation
        Exit Sub
    End If
    For d = CDate(s) To DateAdd("m", 1, CDate(s)) - 1
        Range("F2").Value = Format(d, "dddd")
        Range("I2").Value = "" & Format(d, "m/d/yyyy")
        ActiveSheet.PrintOut
    Next d
End Sub

提前干杯:)

标签: excelvbaprintingdateadd

解决方案


您可以使用工作日功能。它返回一个从 1 到 7 的数字。您只能过滤工作日的 1-5。

Sub PrintSheet()
    Dim s As String
    Dim d As Date
    s = InputBox(Prompt:="Please enter the start date")
    If Not IsDate(s) Then
        MsgBox "Incorrect date", vbExclamation
        Exit Sub
    End If
    For d = CDate(s) To DateAdd("m", 1, CDate(s)) - 1
        If Weekday(d, vbMonday) < 6 Then
        Range("F2").Value = Format(d, "dddd")
        Range("I2").Value = "" & Format(d, "m/d/yyyy")
        'MsgBox ("printing ") 'for testing
        ActiveSheet.PrintOut
        End If
    Next d
End Sub

推荐阅读