Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 14478|回复: 26

[函数-VBA对对碰]datedif与datediff(日期间隔计算)

[复制链接]
发表于 2008-2-11 22:22 | 显示全部楼层 |阅读模式
<div style="MARGIN-TOP: 10px; FONT-SIZE: 10pt; MIN-HEIGHT: 200px; WORD-BREAK: break-all; TEXT-INDENT: 24px; LINE-HEIGHT: normal; WORD-WRAP: break-word;"><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">前言:</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">excel</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">某些工作表函数功能在</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">同样也可以实现,但具体怎么实现呢?有哪些工作表函数可以在</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">语句中直接使用呢!为了让</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">初学者了解在工作表中实现的功能在</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">中如何实现,兰色特把有相似或相同功能的</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">excel</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">函数和</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">函数或语句进行对比说明,构成了</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">“VBA-Excel</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">函数对对碰系列</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">”</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">。希望大家能够喜欢。</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">本期对比内容:</span><span lang="EN-US" style="FONT-SIZE: 18pt; COLOR: #d60808; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">Datedif</span><span lang="EN-US" style="FONT-SIZE: 18pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;</span><span style="FONT-SIZE: 18pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、</span><span lang="EN-US" style="FONT-SIZE: 18pt; COLOR: #0f0fce; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">Datedif</span><span lang="EN-US" style="FONT-SIZE: 18pt; COLOR: red; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">f</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: red; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> </span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"><p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">实现的功能:</span><b><span style="FONT-SIZE: 10pt; COLOR: #1515b8; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">计算起始日期的间隔年</span></b><b><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #1515b8; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt;">\</span></b><b><span style="FONT-SIZE: 10pt; COLOR: #1515b8; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">月</span></b><b><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #1515b8; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt;">\</span></b><b><span style="FONT-SIZE: 10pt; COLOR: #1515b8; FONT-FAMILY: 宋体; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">日数</span></b><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">。</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">实例:</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、计算</span><chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2007-8-2</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">至</span><chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2008-2-1</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">的月份数</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <span lang="EN-US"><p></p></span></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在工作表中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #de1010; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DATEDIF("<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>2007-8-2<chsdate></chsdate>","<chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate>2008-2-1<chsdate></chsdate>","m")&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #de1010; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">5</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">代码中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DateDiff("m", #<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>8/2/2007<chsdate></chsdate>#, #2/1/2008#)&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">6 <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2</span><span style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">计算</span><chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2007-8-2</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">至</span><chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2008-2-1</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">的天数</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> </span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"><p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在工作表中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #de1010; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DATEDIF("<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>2007-8-2<chsdate></chsdate>","<chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate>2008-2-1<chsdate></chsdate>","D")&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #de1010; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">183</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">代码中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DateDiff("D", #<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>8/2/2007<chsdate></chsdate>#, #2/1/2008#)&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">183 <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、<span style="COLOR: black;">计算</span></span><chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2007-8-2</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">至</span><chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">2008-2-1</span><chsdate></chsdate><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">的年数</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> </span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"><p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在工作表中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #de1010; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DATEDIF("<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>2007-8-2<chsdate></chsdate>","<chsdate year="2008" day="1" islunardate="False" isrocdate="False" wst="on"></chsdate>2008-2-1<chsdate></chsdate>","Y")&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #ee0000; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">0</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">在</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">代码中:</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">=DateDiff("</span><b><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #d63108; FONT-FAMILY: Arial; mso-bidi-font-size: 12.0pt; mso-font-kerning: 0pt;">YYYY</span></b><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">", #<chsdate year="2007" day="2" islunardate="False" isrocdate="False" wst="on"></chsdate>8/2/2007<chsdate></chsdate>#, #2/1/2008#)&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">结果为<font color="#0f0fce">1</font></span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan;"><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">简评</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: #0707b5; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">: </span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">二者参数相似</span><span lang="EN-US" style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">,</span><span style="FONT-SIZE: 10pt; COLOR: black; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">但有以下不同:</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <span lang="EN-US"><p></p></span></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、参数顺序不同</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、在计算年和月的间隔数且不为整年或整月时,</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">VBA</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">中的</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">datedif<span style="COLOR: #ee0000;">f</span></span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">计算的结果往往比工作表函数</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">datedif</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">计算出的值大</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">1</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">。</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、年月日代码不尽相同,详参阅帮助文件</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p class="MsoNormal" align="left" style="MARGIN: 0cm 0cm 0pt; WORD-BREAK: break-all; TEXT-INDENT: 18pt; TEXT-ALIGN: left; mso-pagination: widow-orphan; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto;"><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;4</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">、</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">datediff</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">可以计算两个日期隔多少个星期(周),而</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;">datedif</span><span style="FONT-SIZE: 10pt; FONT-FAMILY: 宋体; mso-font-kerning: 0pt; mso-ascii-font-family: Arial; mso-hansi-font-family: Arial; mso-bidi-font-family: Arial;">不能。</span><span lang="EN-US" style="FONT-SIZE: 10pt; FONT-FAMILY: Arial; mso-font-kerning: 0pt;"> <p></p></span></p><p></p><p></p><p>&nbsp;</p></div>
[此贴子已经被作者于2008-2-11 22:26:55编辑过]
发表于 2008-2-11 22:25 | 显示全部楼层
回复

使用道具 举报

发表于 2008-2-11 22:28 | 显示全部楼层

<p>谢谢兰版!!!!!!!</p><p>星期(周) 怎么写 ?</p>
回复

使用道具 举报

 楼主| 发表于 2008-2-11 22:31 | 显示全部楼层

<div class="msgheader">QUOTE:</div><div class="msgborder"><b>以下是引用<i>lllllllll</i>在2008-2-11 22:28:00的发言:</b><br/><p>谢谢兰版!!!!!!!</p><p>星期(周) 怎么写 ?</p></div><p>间隔周数比较复杂,还要考虑到后面的参数. 具体看VBA帮助吧.</p>
回复

使用道具 举报

 楼主| 发表于 2008-2-11 22:32 | 显示全部楼层

datediff参数说明(摘自VBA帮助): <table cellspacing="4" cols="2" cellpadding="4"><tbody><tr valign="top"><th width="14%">设置</th><th width="86%">描述</th></tr><tr valign="top"><td class="T" width="14%">yyyy</td><td class="T" width="86%">年</td></tr><tr valign="top"><td class="T" width="14%">q</td><td class="T" width="86%">季</td></tr><tr valign="top"><td class="T" width="14%">m</td><td class="T" width="86%">月</td></tr><tr valign="top"><td class="T" width="14%">y</td><td class="T" width="86%">一年的日数</td></tr><tr valign="top"><td class="T" width="14%">d</td><td class="T" width="86%">日</td></tr><tr valign="top"><td class="T" width="14%">w</td><td class="T" width="86%">一周的日数</td></tr><tr valign="top"><td class="T" width="14%">ww</td><td class="T" width="86%">周</td></tr><tr valign="top"><td class="T" width="14%">h</td><td class="T" width="86%">时</td></tr><tr valign="top"><td class="T" width="14%">n</td><td class="T" width="86%">分钟</td></tr><tr valign="top"><td class="T" width="14%">s</td><td class="T" width="86%">秒</td></tr></tbody></table><br/><p></p><p class="T"><b><i>firstdayofweek </i></b>参数的设定值如下:</p><table cellspacing="4" cols="3" cellpadding="4"><tbody><tr valign="top"><th width="26%">常数</th><th width="12%">值</th><th width="62%">描述</th></tr><tr valign="top"><td class="T" width="26%"><b>vbUseSystem</b></td><td class="T" width="12%">0</td><td class="T" width="62%">使用 NLS API 设置。</td></tr><tr valign="top"><td class="T" width="26%"><b>vbSunday</b></td><td class="T" width="12%">1</td><td class="T" width="62%">星期日(缺省值)</td></tr><tr valign="top"><td class="T" width="26%"><b>vbMonday</b></td><td class="T" width="12%">2</td><td class="T" width="62%">星期一</td></tr><tr valign="top"><td class="T" width="26%"><b>vbTuesday</b></td><td class="T" width="12%">3</td><td class="T" width="62%">星期二</td></tr><tr valign="top"><td class="T" width="26%"><b>vbWednesday</b></td><td class="T" width="12%">4</td><td class="T" width="62%">星期三</td></tr><tr valign="top"><td class="T" width="26%"><b>vbThursday</b></td><td class="T" width="12%">5</td><td class="T" width="62%">星期四</td></tr><tr valign="top"><td class="T" width="26%"><b>vbFriday</b></td><td class="T" width="12%">6</td><td class="T" width="62%">星期五</td></tr><tr valign="top"><td class="T" width="26%"><b>vbSaturday</b></td><td class="T" width="12%">7</td><td class="T" width="62%">星期六</td></tr></tbody></table><br/><p></p><p class="T"></p><table cellspacing="4" cols="3" cellpadding="4"><tbody><tr valign="top"><th width="26%">常数</th><th width="12%">值</th><th width="62%">描述</th></tr><tr valign="top"><td class="T" width="26%"><b>vbUseSystem</b></td><td class="T" width="12%">0</td><td class="T" width="62%">用 NLS API 设置。</td></tr><tr valign="top"><td class="T" width="26%"><b>vbFirstJan1</b></td><td class="T" width="12%">1</td><td class="T" width="62%">从包含 1 月 1 日的星期开始(缺省值)。</td></tr><tr valign="top"><td class="T" width="26%"><b>vbFirstFourDays</b></td><td class="T" width="12%">2</td><td class="T" width="62%">从第一个其大半个星期在新的一年的一周开始。</td></tr><tr valign="top"><td class="T" width="26%"><b>vbFirstFullWeek</b></td><td class="T" width="12%">3</td><td class="T" width="62%">从第一个无跨年度的星期开始。</td></tr></tbody></table><br/><p></p><p><b>说明</b></p><p class="T"><b>DateDiff</b> 函数可用来决定两个日期之间所指定的时间间隔数目。例如,可以使用 <b>DateDiff</b> 来计算两个日期之间相隔几日,或计算从今天起到年底还有多少个星期。</p><p class="T">为了计算 <b><i>date1</i></b> 与 <b><i>date2 </i></b>相差的日数,可以使用“一年的日数”(y) 或“日”(d)。当 <b><i>interval</i></b> 是“一周的日数”(w) 时,<b>DateDiff</b> 返回两日期间的周数。如果 <b><i>date1</i></b> 是星期一,<b>DateDiff</b> 计算到 <b><i>date2</i></b> 为止的星期一的个数。这个数包含 <b><i>date2</i></b> 但不包含 <b><i>date1</i></b>。不过,如果 <b><i>interval</i></b> 是“周”(ww),则 <b>DateDiff</b> 函数返回两日期间的“日历周”数。由计算 <b><i>date1</i></b> 与 <b><i>date2 </i></b>之间星期日的个数而得。如果 <b><i>date2 </i></b>刚好是星期日,则 <b><i>date2</i></b> 也会被加进 <b>DateDiff </b>的计数结果中;但不论 <b><i>date1 </i></b>是否为星期日,都不将它算进去。</p><p class="T">如果 <b><i>date1 </i></b>比 <b><i>date2 </i></b>来得晚,则 <b>DateDiff</b> 函数的返回值为负数。</p><p class="T"><b><i>firstdayofweek</i></b> 参数会影响使用时间间隔符号 “W” 或 “WW” 计算的结果。</p><p class="T">如果 <i>date1 </i>或 <i>date2 </i>是日期文字,则指定的年份成为该日期的固定部分。但是,如果 <i>date1 </i>或 <i>date2 </i>用双引号 (" ") 括起来,且年份略而不提,则在每次计算表达式 <i>date1 </i>或 <i>date2 </i>时,当前年份都会插入到代码之中。这样就可以书写适用于不同年份的程序代码。</p><p class="T">在计算 12 月 31 日和来年的 1 月 1 日的年份差时,<b>DateDiff</b> 返回 1 表示相差一个年份,虽然实际上只相差一天而已。</p>
[此贴子已经被作者于2008-2-11 22:32:46编辑过]
回复

使用道具 举报

发表于 2008-2-11 22:33 | 显示全部楼层

天数/7 行吗?[em04]
回复

使用道具 举报

发表于 2008-2-12 20:28 | 显示全部楼层

[em23]
回复

使用道具 举报

发表于 2008-2-13 08:14 | 显示全部楼层

<p>收藏学习了。谢谢兰版!!!</p>
回复

使用道具 举报

发表于 2008-2-13 08:41 | 显示全部楼层

<p>工作表里熟悉的东东,到了VBA里就似曾相识,又不太相识!</p>[em04]
回复

使用道具 举报

发表于 2008-4-29 16:46 | 显示全部楼层

<p>这个要好好思考一下~~再回去。。看看~!!</p><p>谢谢兰老师~!</p>
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-6-29 05:04 , Processed in 0.177647 second(s), 7 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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