设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2151|回复: 3
打印 上一主题 下一主题

[Access本身] [原创]统计某个日期区间内星期天的个数的函数

[复制链接]
跳转到指定楼层
1#
发表于 2006-1-3 08:39:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
文章简介:
算出某个日期区间内星期天的个数的函数

文章正文:
'功能:算出某个日期区间内星期天的个数
'作者:竹笛
'修改历史:2005/12/30
'未经严格测试,有问题请与作者交流

Function SundayCount(StartDate As Date, EndDate As Date) As Long
    On Error GoTo Err_SundayCount:
    Dim Days As Integer    '区间天数
    Dim FirstSunday As Date    '第一个周日具体日期
    Dim NextSunday As Date    '下一个周日具体日期
    Dim Myweekday As Integer
    Dim i As Long
    Dim j As Long
    '确保日期都不为空,若为空则置为0
    If Not IsNull(StartDate) And Not IsNull(EndDate) Then
    '如果结束日期<开始日期,则为0
        If EndDate >= StartDate Then
            '如果天数大于7,则先确定第一个周日是哪个日期,再7天一加,直到大于结束日期
            Days = EndDate - StartDate
            ' If Days > 7 Then
            Myweekday = Weekday(StartDate)    '算出是周几,星期天是1
            If Myweekday > 1 Then
                FirstSunday = StartDate + 8 - Myweekday
            Else
                FirstSunday = StartDate
            End If
            Debug.Print "最近的周日是: " & FirstSunday
            NextSunday = FirstSunday + 7
            i = 1
            SundayCount = 1
            For i = 1 To Days Step 7
                Debug.Print "下一个周日是: " & NextSunday
                If NextSunday > EndDate Then
                    If FirstSunday > EndDate Then
                        SundayCount = SundayCount - 1
                    End If
                    Debug.Print "周日数目是: " & SundayCount
                    Exit Function
                End If
                NextSunday = NextSunday + 7
                i = i + 1
                SundayCount = SundayCount + 1
                Debug.Print "周日数目是: " & SundayCount
            Next
        Else
            SundayCount = 0
        End If
    Else
        SundayCount = 0
    End If
Exit_SundayCount:
    Exit Function
Err_SundayCount:
    SundayCount = 0
    Resume Exit_SundayCount
End Function

Sub Test()
Debug.Print SundayCount(#2/6/2005#, #2/25/2005#)
End Sub
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
 楼主| 发表于 2006-1-3 08:40:00 | 只看该作者
抽空发个帖子,免得大家把偶忘了:)
3#
发表于 2013-5-17 10:29:38 | 只看该作者
你好!我将不相关的<BR>去除后代入公式无效。请明示或发个示例共享。多谢!!!!
4#
发表于 2013-5-17 10:32:25 | 只看该作者
Option Compare Database

Function SundayCount(StartDate As Date, EndDate As Date) As Long  ‘'统计某个日期区间内星期天的个数的函数


On Error GoTo Err_SundayCount
Dim Days As Integer    '区间天数
Dim FirstSunday As Date    '第一个周日具体日期
Dim NextSunday As Date    '下一个周日具体日期
Dim Myweekday As Integer
Dim i As Long
Dim j As Long   '确保日期都不为空,若为空则置为0
If Not IsNull(StartDate) And Not IsNull(EndDate) Then    '如果结束日期&lt;开始日期,则为0
If EndDate &gt;= StartDate Then            '如果天数大于7,则先确定第一个周日是哪个日期,再7天一加,直到大于结束日期
Days = EndDate - StartDate
' If Days &gt; 7 Then           Myweekday = Weekday(StartDate)    '算出是周几,星期天是1
If Myweekday &gt; 1 Then     FirstSunday = StartDate + 8 - Myweekday
Else
FirstSunday = StartDate
End If           Debug.Print "最近的周日是: " &amp; FirstSunday            NextSunday = Fir
stSunday + 7    i = 1    SundayCount = 1     For i = 1 To Days Step 7     Debug.Print "下一个周日是: " &amp; NextSunday
If NextSunday &gt; EndDate Then
If FirstSunday &gt; EndDate Then                       SundayCount = SundayCount - 1
End If
Debug.Print "周日数目是: " & amp; SundayCount
Exit Function
End If
NextSunday = NextSunday + 7   i = i + 1   SundayCount = SundayCount + 1   Debug.Print "周日数目是: " &amp; SundayCount           Next
Else
SundayCount = 0
End If
Else: SundayCount = 0
End If
Exit_SundayCount:   Exit Function
Err_SundayCount:    SundayCount = 0   Resume Exit_SundayCount
End Function
Sub Test()  Debug.Print SundayCount(#2/6/2005#, #2/25/2005#)
End Sub


以上的代码不知对否???

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

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-9-21 20:26 , Processed in 0.089767 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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