作者:绿色风
分类:
时间:2022-08-18
浏览:97
楼主 DJ_Soo |
Q:如何找到对应的weeknum? 我使用的weeknum和Excel提供的weeknum不同,主要区别在于一年的第一周的定义: 比如我用outlook可以用工具-选项-日历选项中设置第一周的判断(如下图),但是Excel不能有这样的判断,怎么办呢?
→ A:ALT+F11→插入模块→在模块中输入以下代码:
- Function WeekNumO(Dt As Date) As Integer
- Dim Fd As Date
- Dim LEd As Date
- Dim Wd As Byte
- Dim Yr As Integer
- Yr = Year(Dt)
- Fd = DateSerial(Yr, 1, 1)
- LEd = DateSerial(Yr, 1, 0)
- With Application
- Wd = IIf((Fd - 1) Mod 7, (Fd - 1) Mod 7, 7)
- If Wd <= 4 Then
- WeekNumO = .RoundUp(((Dt - Fd + Wd)) / 7, 0)
- Else
- WeekNumO = .RoundUp(((Dt - Fd + Wd)) / 7, 0) - 1
- If WeekNumO = 0 Then WeekNumO = WeekNumO(LEd)
- End If
- End With
- End Function
今天是2010-6-7,我输入=weeknumo(today())得到的是23而不是24。这样就符合我的要求了 |
2楼 DJ_Soo |
更新,加入年份:- Function WeekNumO(Dt As Date, Optional sty As String) As Long
- Dim Fd As Date
- Dim LEd As Date
- Dim Wd As Byte
- Dim Yr As Integer
- Yr = Year(Dt)
- Fd = DateSerial(Yr, 1, 1) '今年第一天
- LEd = DateSerial(Yr, 1, 0) '去年最后一天
- With Application
- Wd = IIf((Fd - 1) Mod 7, (Fd - 1) Mod 7, 7) '周天的情况,会返回0,则改成7(weekday)
- If Wd <= 4 Then '第一天是周四之前直接/7向上取整得到周号
- WeekNumO = .RoundUp(((Dt - Fd + Wd)) / 7, 0)
- If sty = "year" Then WeekNumO = Year(Fd) & Format(WeekNumO, "00")
- Else
- WeekNumO = .RoundUp(((Dt - Fd + Wd)) / 7, 0) - 1 '第一天是周四之后直接/7向上取整得到周号-1
- If WeekNumO = 0 Then 'WeekNumO = WeekNumO(LEd) '跨年的情况取去年年份
- If sty = "year" Then
- WeekNumO = Year(LEd) & Format(WeekNumO, "00")
- Else
- WeekNumO = WeekNumO(LEd)
- End If
- Else '不跨年就取今年的年份
- If sty = "year" Then
- WeekNumO = Year(Fd) & Format(WeekNumO, "00")
- End If
- End If
- End If
- End With
- End Function
|
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一