Excel VBA 编程基础 -- 日期与时间(三)- 日期(三)
- 2026-11-02 02:11:38
Excel VBA 编程基础 -- 日期与时间(三)- 日期(三)9. DateSerial 函数 DateSerial 函数由年、月、日来构造日期。其一般格式如下: DateSerial(year, month, day) 其中,year、month、day 均为 Integer 类型的整数: DateSerial 的返回值: 例1. DateSerial 的简单应用 如果 year 参数为两位数字,则对应的年份如下: 例2. 不同年份举例 前面说过,year 的取值范围是 100 - 9999,但也可以为 0 - 99,对应关系如上所述。month 的取值范围是 1- 12,day 的取值范围是 1 - 31,但都可以超越自己的范围,只要不超出 Integer 的取值范围,即 -32768 - 32767。如果超出 Integer 的取值范围,则报错。 10. DateValue 函数 一般来讲,DateValue 是将一个字符串形式表示的日期转换为一个 Variant (Date) 类型的日期。其一般格式为: DateValue(dateexpr) 其中,dateexpr 是一个字符串形式的日期表达式。 DateValue 的返回值: 例1. DateValue 函数的简单应用 11. Year 函数 Year 函数返回日期中的年份,其一般格式如下: Year(date) 其中,date 是一个日期表达式。其返回值为 Variant(Integer)。 例1. Year 函数的简单应用 12. Month 函数 Month 函数返回日期中的月份,其一般格式如下: Month(date) 其中,date 是一个日期表达式。其返回值为 Variant(Integer)。 例1. Month 函数的简单应用 13. MonthName 函数 MonthName 函数返回月份的名字,其一般格式如下: MonthName(month[, abbreviate]) 其中,month 是数值形式的月份,abbreviate 是可选参数,如果为 True,则返回月份的简称,如果省略,或为 False,则返回月份的全称。 返回值:字符串形式的月份名称。 例1. MonthName 函数的简单应用 MonthName 函数返回的月份名称是英文名称、中文名称或其他什么名称,取决于 Windows 系统中的时间和语言设置。 14. Day 函数 Day 函数返回日期中的日,其一般格式如下: Day(date) 其中,date 是一个日期表达式。其返回值为 Variant (Integer)。 例1. Day 函数的简单应用 15. IsDate 函数 IsDate 函数判断一个表达式是否合法的日期表达式,其一般格式如下: IsDate(expr) 其中,expr 是一个日期表达式或字符串表达式。 IsDate 的返回值: 例1. IsDate 函数的简单应用 相关阅读 Excel VBA 编程基础 -- 日期与时间(一)- 日期(一) Excel VBA 编程基础 -- 日期与时间(二)- 日期(二)
year:范围在 100 - 9999 month:范围在 1 - 12 day:范围在 1 - 31
类型:Variant (Date) 意义:日期
Dim myDate As VariantmyDate = DateSerial(1999, 1, 2)Debug.Print myDate ' 1999-01-02
00 - 49:对应于 2000 - 2049 50 - 99:对应于 1950 - 1999
Debug.Print DateSerial(10, 10, 12) ' 2010-10-12Debug.Print DateSerial(80, 10, 12) ' 1980-10-12Debug.Print DateSerial(100, 10, 12) ' 100-10-12Debug.Print DateSerial(100, 3500, 1) ' 391-08-01Debug.Print DateSerial(100, 3500, 1200) ' 394-11-12Debug.Print DateSerial(100, 35000, 1) ' ERROR!
类型:Variant (Date) 意义:日期
Dim myDate As VariantmyDate = DateValue("2026-04-17")myDate = DateValue("2026年4月17日")myDate = DateValue("April 17, 2026")myDate = DateValue("Apr 17, 2026")myDate = DateValue("Ap 17, 2026") ' ERROR
Debug.Print Year(DateValue("2026-04-17")) ' 2026Debug.Print Year(DateSerial(2026, 4, 17)) ' 2026Debug.Print Year(#2026-04-17#) ' 2026Debug.Print Year(#4/17/2026#) ' 2026
Debug.Print Month(DateSerial("2026-04-17")) ' 4Debug.Print Month(DateValue(2026, 4, 17)) ' 4Debug.Print Month(#2026-04-17#) ' 4Debug.Print Month(#4/17/2026#) ' 4
Debug.Print MonthName(Month(#2026-04-17#)) ' 四月Debug.Print MonthName(Month(#4/17/2026#), False) ' AprilDebug.Print MonthName(Month(#4/17/2026#), True) ' Apr
Debug.Print Day(DateValue("2026-04-17")) ' 17Debug.Print Day(DateSerial(2026, 4, 17)) ' 17Debug.Print Day(#2026-04-17#) ' 17Debug.Print Day(#4/17/2026#) ' 17
类型:Boolean 意义:True 表示 expr 是一个合法的日期表达式,False 表示 expr 不是一个合法的日期表达式
Dim expr As StringDim valid As Booleanexpr = "2026年4月17日"valid = IsDate(expr) ' Trueexpr = "2026年4月71日"valid = IsDate(expr) ' Falseexpr = DateSerial(2026, 4, 71)valid = IsDate(expr) ' True
本文来自网友投稿或网络内容,如有侵犯您的权益请联系我们删除,联系邮箱:wyl860211@qq.com 。