ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 8956|回复: 7

[原创]在VBA中使用Datedif函数求时间间隔

[复制链接]

TA的精华主题

TA的得分主题

发表于 2007-5-21 16:14 | 显示全部楼层 |阅读模式
A1单元格内容为:2005-9-8,B3单元格内容为:2007-4-5,要求出它们间隔的年、月、日,在工作表中可以使用Datedif函数,而且结果非常精确,但在VBA中却比较麻烦。因为Datedif函数不能直接在VBA中使用,而使用VBA提供的函数却不能得到精确的计算结果。于是很多人在写代码时就自编一个函数来实现Datedif函数的功能,其实借助VBAEvaluate函数我们同样可以很方便地在VBA中使用Datedif函数:
一、求单元格时间间隔:
A1单元格内容为:2005-9-8B3单元格内容为:2007-4-5
Sub test1()
    MsgBox "间隔" & Application.Evaluate("=Datedif(a1,b3,""y"")") & ""
    MsgBox "间隔" & Application.Evaluate("=Datedif(a1,b3,""m"")") & ""
    MsgBox "间隔" & Application.Evaluate("=Datedif(a1,b3,""d"")") & ""
End Sub
二、求两个TextBox的时间间隔:
TextBox1单元格内容为:2005-9-8TextBox2单元格内容为:2007-4-5
       Sub test2()
           MsgBox "=Datedif(""" & TextBox1.Text & """,""" & TextBox2.Text & """,""y"")"
          MsgBox "间隔" & Application.Evaluate("=Datedif(""" & TextBox1.Text & """,""" & TextBox2.Text & """,""y"")") & ""
    End Sub
三、求两个变量的时间间隔:
       Sub test3()
         Dim time1, time2
         time1 = "2005-9-8"
         time2 = "2007-4-7"
        MsgBox "间隔" & Application.Evaluate("=Datedif(""" & time1 & """,""" & time2 & """,""y"")") & ""
End Sub

注:以上内容未在Office2007中进行测试!

TA的精华主题

TA的得分主题

发表于 2007-5-21 17:30 | 显示全部楼层

谢谢分享。其实VBA自带DATEDIFF函数,供参考:

DateDiff Function

      

Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

The DateDiff function syntax has these named arguments:

PartDescription
intervalRequired. String expression that is the interval of time you use to calculate the difference between date1 and date2.
date1, date2Required; Variant (Date). Two dates you want to use in the calculation.
firstdayofweekOptional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyearOptional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.

Settings

The interval argument has these settings:

SettingDescription
yyyyYear
qQuarter
mMonth
yDay of year
dDay
wWeekday
wwWeek
hHour
nMinute
sSecond

The firstdayofweek argument has these settings:

ConstantValueDescription
vbUseSystem0Use the NLS API setting.
vbSunday1Sunday (default)
vbMonday2Monday
vbTuesday3Tuesday
vbWednesday4Wednesday
vbThursday5Thursday
vbFriday6Friday
vbSaturday7Saturday

ConstantValueDescription
vbUseSystem0Use the NLS API setting.
vbFirstJan11Start with week in which January 1 occurs (default).
vbFirstFourDays2Start with the first week that has at least four days in the new year.
vbFirstFullWeek3Start with first full week of the year.

Remarks

You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year.

To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday.

If date1 refers to a later point in time than date2, the DateDiff function returns a negative number.

The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols.

If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years.

When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed.

Note   For date1 and date2, if the Calendar property setting is Gregorian, the supplied date must be Gregorian. If the calendar is Hijri, the supplied date must be Hijri.

TA的精华主题

TA的得分主题

 楼主| 发表于 2007-5-21 17:40 | 显示全部楼层

DateDiff 计算结果没有Datedif准确:

在计算 12 月 31 日和来年的 1 月 1 日的年份差时,DateDiff 返回 1 表示相差一个年份,虽然实际上只相差一天而已.

若财务上按1年工龄发工资恐怕要陪钱咯!

TA的精华主题

TA的得分主题

发表于 2011-6-24 11:17 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2011-12-6 13:38 | 显示全部楼层
extyg 发表于 2007-5-21 17:40
DateDiff 计算结果没有Datedif准确:在计算 12 月 31 日和来年的 1 月 1 日的年份差时,DateDiff 返回 1 表示 ...

对啊,还是这个函数实用,不懂会计的人就说有自带的DATEDIFF函数

TA的精华主题

TA的得分主题

发表于 2011-12-6 13:39 | 显示全部楼层
northwolves 发表于 2007-5-21 17:30
谢谢分享。其实VBA自带DATEDIFF函数,供参考:DateDiff Function      Retu ...

这个函数一点也不实用,是个垃圾函数

TA的精华主题

TA的得分主题

发表于 2012-11-16 22:26 | 显示全部楼层

TA的精华主题

TA的得分主题

发表于 2017-9-20 13:41 | 显示全部楼层
楼主暖男,好贴,标记一下
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-4-27 05:05 , Processed in 0.052863 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表