我如何根据用户输入生成日期列表?

How could I generate a list of dates based on user input?

假设我有一个电子表格,允许用户输入如下元数据:

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/2019end date = 04/30/2019y,列表将如下所示:

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 但可以想象用那个可以做到这一点。

如果我想通过使用像 Pyxll 这样的插件在 Python 中编写这个,是否需要每个拥有 Excel 文件副本的人来安装 [=19] =]?

@simplycoding:VBA 没有一个函数或 suroutine 可以立即完成您正在寻找的事情。但是,您可以根据 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。一开始它看起来很烦人,但在测试和调试代码时它会帮助你很多。

祝你好运!

我被你要求根本不使用 VBA 的挑战所困扰,所以我正在尝试一些 Excel 函数,这就是我希望你尝试的。

我在以下单元格中使用了您的元数据:

选项 1 - 简单

将以下数组函数输入单元格 D1 (Ctrl+Shift+Enter) 并将其一直拖到右侧(例如,拖到单元格 AW1):

=IF(AND(ISNUMBER(MATCH(WEEKDAY($B+COLUMN()-4,2),--($B:$B="y")*(ROW($B:$B)-2),0)),($B+COLUMN()-4)<=$B),$B+COLUMN()-4,"")

显示开始日期和结束日期之间以及所需工作日的所有日期。但是,此选项不会阻止间隙,因此中间会有很多空白列。

选项 2 - 完成

将以下数组函数输入单元格 D2 (Ctrl+Shift+Enter) 并将其一直拖到右侧:

=IFERROR(SUMPRODUCT(SMALL(($B+ROW($B:$B)-1)*(IF(ISNUMBER(MATCH(WEEKDAY($B+ROW($B:$B)-1,2),(--($B:$B="y")*(ROW($B:$B)-2)),0)),1,0)),30-COUNT(MATCH(WEEKDAY($B+ROW($B:$B)-1,2),(--($B:$B="y")*(ROW($B:$B)-2)),0))+COLUMN()-3)),"")

如您所见,此选项显示所有 "proper" 日期并排显示,没有任何间隔。

希望您会发现这对您有所帮助 - 我在使用第二个选项时玩得很开心,我相信它仍然可以改进。

保重, 贾斯蒂娜