excel - Excel VBA - 从自动时间表打印中删除周末
问题描述
我稍微修改了一些我在网上找到的代码。
目的:
单击“打印日期”按钮,然后输入开始日期,并让 Excel 自动生成/打印一个月的时间表(比以前的电子表格要打印 6 周的页面要好得多,而且您必须手动编辑每个日期)。
问题:
它在周末打印,这会浪费纸张。有没有办法可以引用日期列表(周末、公共假期),而不是生成用于打印的日期列表?
您会在代码中看到日期格式为 m/d/yyyy,奇怪地打印为 dd/mm/yyyy(这正是我想要的)。当代码为 dd/mm/yyyy 时,它可以正确打印(2019 年 3 月 20 日),但如果到下个月,它会切换到美国格式 m/d/yyyy(2019 年 4 月 20 日)。我知道这似乎没有意义,但是将它作为 m/d/yyyy 实际上在任何开始/结束日期打印为 dd/mm/yyyy。我想知道为什么,并且代码中的 dd/mm/yyyy 也可以在任何日期范围内正确打印。
代码:
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
提前干杯:)
解决方案
您可以使用工作日功能。它返回一个从 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
推荐阅读
- r - 破解 R Markdown 或 Bookdown 以包含出现在 html 或 docx 输出中的 LaTeX 环境?
- algorithm - 如何估计这个任务属于 NP 类?
- python - 为什么 numpy 和 python list 上的相同操作会得到不同的结果?
- html - CSS:检查不起作用
- pytorch - 如何在多次运行中重现 RNN 结果?
- networking - 牢记网络架构,为什么 Redis Sentinel 不是一个好的架构。但是设置外部 zookeeper 对生产 Solr 有好处
- python - 如何在矩阵中找到元素 == 0 的索引?每行和每列应该只出现一次
- c# - 使用 IIS 10.0 和 WebAPI 进行 IIS 应用程序初始化
- plotly - 如何使用 plotly 创建可滚动的热图?
- java - 如何在 servlet 中进行 id 重复检查