0

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

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

4

2 回答 2

1

@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在编码时使用。起初它可能看起来很烦人,但它会在测试和调试代码时帮助你很多。

祝你好运!

于 2019-03-15T18:02:11.637 回答
0

您要求我根本不使用 VBA,这对我提出了挑战,所以我在玩一些 Excel 函数,这就是我希望您尝试的方法。

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

元数据

选项 1 - 简单

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

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

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

选项 2 - 完成

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

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

如您所见,此选项将所有“正确”日期彼此相邻显示,没有任何间隙。

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

保重,贾斯蒂娜

于 2019-03-16T15:13:27.863 回答