excel - 如何根据用户输入生成日期列表?
问题描述
假设我有一个电子表格,允许用户输入一些元数据,如下所示:
Date range start: mm/dd/yyyy
Date range end: mm/dd/yyyy
Mondays: (y/n)
Tuesdays: (y/n)
Wednesdays: (y/n)
Thursdays: (y/n)
Fridays: (y/n)
基于此,我想生成一个日期列表,格式为mm/dd/yyyy
开始于4/1/2019
,结束于4/30/2019
并且仅包括用 . 表示的日期y
。
因此,如果用户仅在星期一和星期三输入start date = 04/01/2019
, end date = 04/30/2019
,y
列表将如下所示:
04/01/2019
04/03/2019
04/08/2019
04/10/2019
04/15/2019
04/17/2019
04/22/2019
04/24/2019
04/29/2019
找不到开始使用的 Excel 函数。我不知道 VBA,但可以想象用它来做到这一点。
如果我想通过使用类似的插件在 Python 中编写这个Pyxll
,是否需要每个拥有 Excel 文件副本的人都安装Pyxll
?
解决方案
@simplycoding:VBA 没有准备好开箱即用的功能或程序来执行您正在寻找的事情。但是,您可以根据 VBA 提供的任何内容编写自己的内容,这很多。
这是我的启动场景:
我在大约 20 分钟内编写、测试并评论了以下 SUB。当我说我不是第一行 VBA 编码器时,请相信我。
Sub DateList()
'(C) Antonio Rodulfo, 2019
Dim dEnd, dStart, dCounter As Date
Dim iaDays(1 To 5) As Integer
Dim iCounter, iRow As Integer
' Indent your sentences properly
' Reading parameters
dStart = Cells(2, 1).Value
dEnd = Cells(2, 2)
For iCounter = 1 To 5
If Cells(2, 2 + iCounter) = "y" Then
iaDays(iCounter) = 1
Else
iaDays(iCounter) = 0
End If
Next iCounter
' Here's where the list of dates will start
iRow = 4
' I process the dates: Excel stores dates in its own
' coding, which we can use to run a for..next loop
For dCounter = dStart To dEnd
' Weekday(datecode,type) returns the day of week
' type 2 means the week starts with monday being day 1
iCounter = Weekday(dCounter, 2)
' The sub only sets dates for working days
' monday to friday
If iCounter <= 5 Then
' date must be set if found "y" in the matching day
If iaDays(iCounter) = 1 Then
' I like setting the proper numberformat so
' that no surprises are found when reading it
Cells(iRow, 1).NumberFormat = "dd/mmm/yyyy"
Cells(iRow, 1) = dCounter
' Increasing iRow sets the focus on next row
iRow = iRow + 1
End If
End If
' Adding the index name to the next sentence helps
' tracking the structures
Next dCounter
End Sub
我总是建议Option Explicit
在编码时使用。起初它可能看起来很烦人,但它会在测试和调试代码时帮助你很多。
祝你好运!
推荐阅读
- django - 如何克隆完整的继承树模型
- c# - C#中没有属性的序列化/反序列化
- node.js - 无效使用类型“未定义”作为 Firestore 参数
- node.js - 平静地响应耗时计算的最佳实践(带 Express 的 Node.js)
- shell - 理解 Unix 差异命令
- html - 使最后一个菜单项背景的颜色不同,以扩展并填充其余空间?
- macos - osx上不同插件中的不同Qt框架版本
- java - 执行 getFilter 时 ViewHolder 错误的行为
- matplotlib - 为什么 matplotlib.pyplot.imshow 不能正确显示颜色?
- amazon-web-services - 如何将请求的标头块从 API Gateway 传递到 Step 函数/lambda?