There were a few unnecessary declarations in the previous version. In
addition, I have modified it so that it handles the period extending over
the end of one year and also the varying number of days in February when
there is a leap year event. I have also added a check on the total of the
amounts for each month.
Dim Date1 As Date, Date2 As Date
Dim Nummonths As Long, startday As Long, endday As Long, startmonth As Long,
numdays As Long
Dim StartMonthDays As Long
Dim amount As Long, amounts As String
Dim monthdays As Long
Dim amountbymonth As Variant
Dim thismonth As Long
Dim Total As Double
amount = 200000
Date1 = Date
Date2 = #11/15/2007#
startmonth = Format(Date2, "m")
startday = Format(Date2, "d")
endday = Format(Date1, "d")
numdays = DateDiff("d", Date2, Date1)
Nummonths = DateDiff("m", Date2, Date1)
Select Case startmonth
Case 1, 3, 5, 7, 8, 10, 12
StartMonthDays = 31
Case 4, 6, 9, 11
StartMonthDays = 30
Case Else
If ((Format(Date2, "yyyy") Mod 4 = 0 And Format(Date2, "yyyy") Mod
400 = 0) Or Format(Date2, "yyyy") Mod 4 = 0 And Format(Date2, "yyyy") Mod
100 <> 0) Then
StartMonthDays = 29
Else
StartMonthDays = 28
End If
End Select
amounts = amounts & Format((StartMonthDays - startday) * amount / numdays,
"#.00")
For i = 1 To Nummonths - 1
thismonth = (startmonth + i) Mod 12
If thismonth = 0 Then
thismonth = 12
End If
Select Case thismonth
Case 1, 3, 5, 7, 8, 10, 12
monthdays = 31
Case 4, 6, 9, 11
monthdays = 30
Case Else
If ((Format(DateAdd("m", i, Date2), "yyyy") Mod 4 = 0 And
Format(DateAdd("m", i, Date2), "yyyy") Mod 400 = 0) Or Format(DateAdd("m",
i, Date2), "yyyy") Mod 4 = 0 And Format(DateAdd("m", i, Date2), "yyyy") Mod
100 <> 0) Then
monthdays = 29
Else
monthdays = 28
End If
End Select
amounts = amounts & "|" & Format(monthdays * amount / numdays, "#.00")
Next i
amounts = amounts & "|" & Format(endday * amount / numdays, "#.00")
amountbymonth = Split(amounts, "|")
For i = 0 To UBound(amountbymonth)
Total = Total + Val(amountbymonth(i))
MsgBox "Amount for " & Format(DateAdd("m", i, Date2), "mmmm yyyy") & "
is $" & amountbymonth(i)
Next i
MsgBox Total 'Check total
--
Hope this helps.
Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.
Doug Robbins - Word MVP, originally posted via msnews.microsoft.com