2011-10-05 44 views
1

如何創建一個類似於DateDiff的查詢/ vba函數,將結果拆分爲每月的天數(即2010年1月1日 - 2010年2月3日= 1月31日,2月: 3(忽略格式))。DateDiff分成幾個月access/vba

+0

因此,每個結果將有兩個日期列表示日期範圍,並且您希望每個結果顯示每個月的天數? – Banjoe

+0

輸入將包含兩個日期列。結果將是12個月,每個月的天數。 (一個DateDiff在所有12個月中分割)。 –

+0

如果不是,則計算本月最後12個月的月份(所以在本月的10月份中,比較從10年前到今天的日期將導致從去年11月初至今的計算) 。 –

回答

1

好吧,我想我明白你想要做什麼。

首先你需要返回一個月的天數的函數,給出年份和月份(你要知道當年佔到由於閏年二月變化的天數):

Function DaysInMonth(month As Integer, year As Integer) As Integer 

    If month < 1 Or month > 12 Then 
     DaysInMonth = -1 
    Else 
     DaysInMonth = Day(DateSerial(year, month + 1, 1) - 1) 
    End If 

End Function 

我已經寫的函數GetMonthDays,是以開始日期和結束日期,並返回整數數組(1到12),其包含在各月的天數,指定的開始和結束日期之間。開始日期和結束日期可以是相隔的任意年數,如果有必要,它將積累每個月的總天數。

例如,函數調用,例如:

Dim months() As Integer 
months = GetMonthDays(#6/13/2011#, #8/1/2011#) 

將返回的數組[0,0,0,0,0,18,31,1,0,0,0,0]

的呼叫,例如:

months = GetMonthDays(#12/25/2010#, #1/15/2011#) 

返回[15,0,0,0,0,0,0,0,0,0,0,7]

在多個年,對於例如:

months = GetMonthDays(#12/25/2009#, #1/15/2011#) 

它會返回[46,28,31,30,31,30,31,31,30,31,30,38]

你可以看到,它已經跨越積累的天數兩個Januarys(31 + 15)和兩個12月(31 + 7)。我不是100%確定這是你想要的,但是如果給定的日期範圍跨越12個月以上,對我來說是有意義的。

基本上,函數循環遍歷開始和結束日期之間的每個月,並累積每個日期。第一個月和最後一個月是需要一些計算的特殊情況,否則它只是該月的天數。

的功能如下,減去錯誤檢查:

Function GetMonthDays(startDate As Date, endDate As Date) As Integer() 

    Dim months(1 To 12) As Integer 
    Dim monthStart As Integer 
    Dim monthEnd As Integer 
    Dim yearStart As Integer 
    Dim yearEnd As Integer 
    Dim monthLoop As Integer 
    Dim yearLoop As Integer 

    ' initialise months array to all zeros 

    For monthLoop = 1 To 12 
     months(monthLoop) = 0 
    Next monthLoop 

    monthStart = month(startDate) 
    monthEnd = month(endDate) 
    yearStart = year(startDate) 
    yearEnd = year(endDate) 

    monthLoop = monthStart 
    yearLoop = yearStart 

    Do Until yearLoop >= yearEnd And monthLoop > monthEnd 

     If yearLoop = yearStart And monthLoop = monthStart Then 
      months(monthLoop) = months(monthLoop) + (DaysInMonth(monthLoop, yearLoop) - Day(startDate) + 1) 
     ElseIf yearLoop = yearEnd And monthLoop = monthEnd Then 
      months(monthLoop) = months(monthLoop) + Day(endDate) 
     Else 
      months(monthLoop) = months(monthLoop) + DaysInMonth(monthLoop, yearLoop) 
     End If 

     If monthLoop < 12 Or (monthLoop = 12 And yearLoop = yearEnd) Then 
      monthLoop = monthLoop + 1 
     Else 
      monthLoop = 1 
      yearLoop = yearLoop + 1 
     End If 

    Loop 

    GetMonthDays = months 

End Function 

我一直在使用功能測試它,例如:

Sub TestRun() 

    Dim months() As Integer 

    months = GetMonthDays(#12/25/2009#, #1/15/2011#) 

    MsgBox _ 
     months(1) & vbCrLf & _ 
     months(2) & vbCrLf & _ 
     months(3) & vbCrLf & _ 
     months(4) & vbCrLf & _ 
     months(5) & vbCrLf & _ 
     months(6) & vbCrLf & _ 
     months(7) & vbCrLf & _ 
     months(8) & vbCrLf & _ 
     months(9) & vbCrLf & _ 
     months(10) & vbCrLf & _ 
     months(11) & vbCrLf & _ 
     months(12) 

End Sub 

這應該是一個很好的起點爲你至少。祝你好運!

+0

編輯:在12月結束日期時進行小的更改以糾正錯誤。 –

+0

對於需要類似功能的其他人,當日期在同一個月(#11/5/2011#,#11/20/2011#返回26)時,上述功能可能會失敗。 –