-ChatGPT4-
-ChatGPT4-に質問
VBAでインプットボックスで入力された数値(月)に基づいて、その月の最終日を日付形式で取得し、指定したセルに代入する。セルの書式はグレゴリオ暦で日付にしてください。
ChatGPT4の回答
Sub GetLastDayOfMonthAndSetToCell()
Dim inputMonth As Integer
Dim lastDay As Date
Dim currentYear As Integer
Dim ws As Worksheet
' 年を取得(現在の年を使用)
currentYear = Year(Date)
' インプットボックスで月を入力
inputMonth = InputBox("月を入力してください(例:4):", "月入力")
' 入力された月の最終日を取得
If inputMonth >= 1 And inputMonth <= 12 Then
lastDay = DateSerial(currentYear, inputMonth + 1, 0)
' 結果をセルに代入(ここでは Sheet1 の A1 セルに代入)
Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Cells(1, 1).Value = lastDay
' セルの書式をグレゴリオ暦に設定
ws.Cells(1, 1).NumberFormat = "[$-F800]dddd, mmmm dd, yyyy"
MsgBox "入力された月の最終日は " & ws.Cells(1, 1).Text & " で、セル A1 に代入されました。"
Else
MsgBox "正しい月を入力してください。"
End If
End Sub

