マクロvba初心者です。年齢計算に付いて教えて下さい。セルA2に誕生日データ yyyy/mm/ddがありB列に2行目から13行目迄が年月yy/mmC列の2行目から13行目迄がその年月の給与金額データがあるとします。この時、当該従業員が65歳になった翌月からの給与金額を加算するには、どの様にしたら宜しいでしょうか?例えば、1960年3月10日生まれの人の場合2025年4月分から12月分迄を加算するものとします。D列以降は、特にデータがないものとします。

1件の回答

回答を書く

1085876

2026-01-04 04:40

+ フォロー

B列がシリアル値でなくyyyy/mmの7文字の文字列の場合です。翌月以降とのことなので、単純に65歳になる年だけ月のみを比較してます。



Sub Sample()

Dim i As Long, n As Long

Dim key1 As Date, tmp

Dim key0 As Range, buf As Range

Set key0 = Range(\u0026quot;A2\u0026quot;)



For i = 2 To 13

Set buf = Cells(i, \u0026quot;B\u0026quot;)

tmp = Split(buf.Value, \u0026quot;/\u0026quot;)

key1 = DateSerial(tmp(0), tmp(1), 1)

Select Case Year(key1) - Year(key0.Value)

Case Is \u0026gt; 65: n = n + Cells(i, \u0026quot;C\u0026quot;).Value

Case 65: If Month(key1) \u0026gt; Month(key0) Then n = n + Cells(i, \u0026quot;C\u0026quot;).Value

End Select

Next i



Range(\u0026quot;D14\u0026quot;).Value = \u0026quot;合計 \u0026quot; \u0026amp; n \u0026amp; \u0026quot; 円\u0026quot;

End Sub

うったえる有益だ(0シェアするブックマークする

関連質問

Copyright © 2026 AQ188.com All Rights Reserved.

博識 著作権所有