假设我有一个电子表格,允许用户输入一些元数据,如下所示:
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,但是想象一下用它来做这件事是可能的。
如果我想使用像Pyxll这样的外接程序来用Python编写这篇文章,那么在安装Pyxll时,是否需要每个拥有Pyxll文件副本的人?
发布于 2019-03-15 18:02:11
@simplycoding: VBA没有现成的函数或suroutine可以立即完成您想要的内容。但是,您可以根据VBA提供的任何内容编写自己的VBA,这是非常多的。
这是我的启动场景:

我在大约20分钟内编写、测试和评论了以下内容。相信我,当我说我不是第一行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-16 15:13:27
你要求我根本不使用VBA,我受到了挑战,所以我在玩一些Excel函数,下面是我想让你尝试的东西。
我在以下单元格中使用了您的元数据:

选项1-简易
在单元格D1 (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 (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)),"")如您所见,此选项将显示彼此之间所有“适当”日期,没有任何空白。
希望你会发现这有帮助-我玩了很多乐趣在第二个选项,我相信它仍然可以改进。
保重,朱斯特娜
https://stackoverflow.com/questions/55183256
复制相似问题