TOP
函数VBA:WeekdayName
描述
VBA WeekdayName 函数根据数字返回星期几的名称。
语法 WeekdayName
WeekdayName(日期)
或者
WeekdayName(日期,该周的第一天)
编号
默认情况下,一周的第一天是星期日 (1),最后一天是星期六 (7)。
要更改编号以使星期一为一周的第一天 (1),星期日为一周的最后一天 (7),请在使用此函数时添加值 2 作为第二个参数:
WeekdayName(日期, 2)
示例 VBA WeekdayName
使用 WeekdayName 函数显示 7 天的名称:
- Sub WeekdayNameExample1()
-
- MsgBox WeekdayName(1)
- MsgBox WeekdayName(2)
- MsgBox WeekdayName(3)
- MsgBox WeekdayName(4)
- MsgBox WeekdayName(5)
- MsgBox WeekdayName(6)
- MsgBox WeekdayName(7)
-
- End Sub
Sub WeekdayNameExample1()
MsgBox WeekdayName(1) '返回:周一
MsgBox WeekdayName(2) '返回:周二
MsgBox WeekdayName(3) '返回:周三
MsgBox WeekdayName(4) '返回:周四
MsgBox WeekdayName(5) '返回:周五
MsgBox WeekdayName(6) '返回:周六
MsgBox WeekdayName(7) '返回:周日
End Sub
WeekdayName 函数还可以通过传递一个值作为第二个参数来显示一天的缩短版本:
- Sub WeekdayNameExample2()
-
- MsgBox WeekdayName(1, True)
- MsgBox WeekdayName(2, True)
- MsgBox WeekdayName(3, True)
- MsgBox WeekdayName(4, True)
- MsgBox WeekdayName(5, True)
- MsgBox WeekdayName(6, True)
- MsgBox WeekdayName(7, True)
-
- End Sub
Sub WeekdayNameExample2()
MsgBox WeekdayName(1, True) '返回:周一。
MsgBox WeekdayName(2, True) '返回:周二。
MsgBox WeekdayName(3, True) '返回:周三。
MsgBox WeekdayName(4, True) '返回:周四。
MsgBox WeekdayName(5, True) '返回:周五。
MsgBox WeekdayName(6, True) '返回:周六。
MsgBox WeekdayName(7, True) '返回:太阳。
End Sub
根据日期显示日期的名称
使用 Weekday 和 WeekdayName 函数,您可以从日期获取星期几的名称:
- Sub WeekdayNameExample3()
-
- MsgBox WeekdayName(Weekday("2020年11月30日", 2))
-
- End Sub
Sub WeekdayNameExample3()
MsgBox WeekdayName(Weekday("2020年11月30日", 2)) '返回:周一
End Sub
虽然直接通过格式化函数FORMAT更容易:
- Sub WeekdayNameExample4()
-
- MsgBox Format("2020年11月30日", "dddd")
-
- End Sub
Sub WeekdayNameExample4()
MsgBox Format("2020年11月30日", "dddd") '返回:周一
End Sub