F023 加算する年の数値を設定して年末を計算する関数

2019-05-20
EXCEL自作関数

 加算する年の数値を設定して年末を計算する関数です。
VBEの標準モジュールに下のコードを登録して「Excelマクロ有効フォーム」で保存してください。
関数の登録方法は⇒♯000 ユーザー定義関数を登録する方法

登録されたら、シート上からユーザー関数「YearEnd」が使用できます。
使い方は 
ワークシート上で = YearEnd (日付,[加算する年の数値])の関数として使用できます。
【例】
 YearEnd(#2019/5/7#) → #2019/12/31#
 YearEnd(#2019/5/7#,1) → #2020/12/31#

Function YearEnd(GDate As String, Optional N As Long) As Variant
’年末を計算する関数、加算する年の数値を設定可能
’YearEnd(日付,[加算月])
’例1 YearEnd(#2019/5/7#) → #2019/12/31#
’例2 YearEnd(#2019/5/7#,1) → #2020/12/31#
Dim Y As Long
Dim M As Long
Dim D As Long
On Error GoTo EXITFEnd
If IsDate(GDate) = True Then
 Y = Year(GDate)
 M = Month(GDate)
 D = Day(GDate)
 YearEnd = DateSerial(Y + N + 1, 1, 0)
Else
 YearEnd = GDate
End If
Exit Function

EXITFEnd:
End Function

ここで紹介したコード使用による損害に対しては一切責任は負えません。