'指定された期間の日付回数を計算する関数 '2月29日の有無には対応していません。 '2月29日の回数を調べようとすると、毎年2月29日があるものとして '計算されます。 'H9.7.18からH12.3.31の間に3月4日と9月4日が何回あるか計算する例です。 ' A B C ' 1 3/4 9/4 ' 2 H9.7.18 H12.3.31 =MyDateCount(A2,B2,A1,B1) Option Explicit Function MyDateCount(startDate As Date, endDate As Date, _ ParamArray dateArray() As Variant) As Variant Dim startNum As Integer, endNum As Integer Dim dateNum As Integer, yearCount As Integer Dim i As Integer, cnt As Integer Dim date1 As Date '引数が不足の場合には#VALUE!を返す If IsMissing(dateArray) Then MyDateCount = CVErr(xlErrValue) Exit Function End If '開始日>終了日の場合には#NUM!を返す If startDate > endDate Then MyDateCount = CVErr(xlErrNum) Exit Function End If '年数を計算 yearCount = Year(endDate) - Year(startDate) - 1 '開始日と終了日を月日で数値化(月*100+日) startNum = Month(startDate) * 100 + Day(startDate) endNum = Month(endDate) * 100 + Day(endDate) '日付回数の初期化 cnt = 0 For i = LBound(dateArray) To UBound(dateArray) If Not IsEmpty(dateArray(i)) Then '日付型変数に代入 date1 = dateArray(i) '日付を月日で数値化(月*100+日) dateNum = Month(date1) * 100 + Day(date1) '日付回数の加算 cnt = cnt + yearCount If dateNum >= startNum Then cnt = cnt + 1 If dateNum <= endNum Then cnt = cnt + 1 End If Next '戻り値の設定 MyDateCount = cnt End Function