excel用自定义函数获取某月中指定日期的数量
时间:2022-05-30 15:32:29
如果我们要获取某月中指定日期的数量,例如,2009年1月中有几个星期一?用Excel内置的日期时间函数无法解决这个问题。我们可以用自定义函数的方法来解决。按Alt+F11打开VBA编辑器,单击菜单“插入→模块”,在右侧的代码窗口中输入自定义函数:
Function WeekDaysInMonth(iYear As Integer, iMonth As Integer, iDay As Integer)
Dim i, iDaysInMonth As Integer
Dim LastDateInMonth As Date
If iMonth < 1 Or iMonth > 12 Or iDay < 1 Or iDay > 7 Then
WeekDaysInMonth = "错误"
Exit Function
End If
iDaysInMonth = day(DateAdd("d", -1, DateSerial(iYear, iMonth + 1, 1)))
LastDateInMonth = DateSerial(iYear, iMonth, iDaysInMonth)
For i = iDaysInMonth – 1 To 0 Step -1
If Weekday(LastDateInMonth – i, vbMonday) = iDay Then
WeekDaysInMonth = WeekDaysInMonth + 1
End If
Next i
End Function
这个自定义函数有3个整数参数,即年、月和代表星期的数值。最后一个参数用1-7的数值表示星期一至星期日。
使用方法是在单元格中输入
=weekdaysinmonth(年,月,星期数值)
例如要获取2009年12月中星期日的数量,在单元格中输入公式:
=weekdaysinmonth(2009,12,7)
公式结果返回4,表示2009年12月中有4个星期日。另外,输入的参数如果超出范围,如输入“=weekdaysinmonth(2009,12,10)”,最后一个参数超出范围,公式返回“错误”。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Excel中如何插入PDF链接
![](https://img.aspxhome.com/file/2023/5/a154125_0s.jpg)
word怎么另存为pdf?
![](https://img.aspxhome.com/file/2023/0/34520_0s.gif)
excel2013制作饼状图的步骤: 首先打开要制作的excel数据
在Word2003中取消Word绘图画布
![](https://img.aspxhome.com/file/2023/1/30571_0s.jpg)
利用Ctrl+enter组合键搞定 批量填充excel合并单元格的序列
![](https://img.aspxhome.com/file/2023/9/a155659_0s.png)
EXCEL复合饼图
最新2023年2月CPU性能天梯图高清版
![](https://img.aspxhome.com/file/2023/5/53155_0s.png)
excel表格里面的图片怎么取出来
如何解决 office 2010 打开报错 无法访问您试图使用的功能所在的网络位置
![](https://img.aspxhome.com/file/2023/1/16511_0s.png)
Word表格中的这些功能很少用却非常牛,第四个方法你肯定没见过
![](https://img.aspxhome.com/file/2023/0/34920_0s.gif)
Excel如何快速删除包含星号(*)的单元格?
![](https://img.aspxhome.com/file/2023/0/a156450_0s.jpg)
word2007工具栏显示.隐藏标记在哪里?
![](https://img.aspxhome.com/file/2023/5/18305_0s.jpg)
如何批量整理图片?巧用WORD EXCEL批量整理网页图片方法介绍
![](https://img.aspxhome.com/file/2023/7/42577_0s.png)
Word文档中行间距调整不了怎么办
![](https://img.aspxhome.com/file/2023/8/31368_0s.jpg)
Excel2016输入数字变成E+17怎么办?Excel2016关闭科学记数教程
![](https://img.aspxhome.com/file/2023/1/40221_0s.jpg)
如何更正 #NUM! 错误
Win10搜索栏如何设置百度搜索?Win10搜索栏设置百度搜索的方法
![](https://img.aspxhome.com/file/2023/6/52776_0s.png)
excel表格中怎么是做导航目录和返回按钮?
![](https://img.aspxhome.com/file/2023/6/41016_0s.png)
win10鼠标灵敏度怎么调详细教程
![](https://img.aspxhome.com/file/2023/8/48328_0s.jpg)
excel如何移动或复制工作表
![](https://img.aspxhome.com/file/2023/0/a155920_0s.png)