土曜日, 10月 19, 2024

Excel VBA 32 
西暦に対する十干十二支を計算


西暦に対する十干十二支を計算してみます。

-------------------
Sub ShowJikkanJyunishi()
Dim year As Integer
Dim jikkan As String
Dim jyunishi As String
Dim inputYear As String
inputYear = InputBox("西暦を入力してください:", "十干十二支")
If IsNumeric(inputYear) Then
year = CInt(inputYear)
' 十干の計算
Select Case (year - 4) Mod 10
Case 0: jikkan = "甲"
Case 1: jikkan = "乙"
Case 2: jikkan = "丙"
Case 3: jikkan = "丁"
Case 4: jikkan = "戊"
Case 5: jikkan = "己"
Case 6: jikkan = "庚"
Case 7: jikkan = "辛"
Case 8: jikkan = "壬"
Case 9: jikkan = "癸"
End Select
' 十二支の計算
Select Case (year - 4) Mod 12
Case 0: jyunishi = "子"
Case 1: jyunishi = "丑"
Case 2: jyunishi = "寅"
Case 3: jyunishi = "卯"
Case 4: jyunishi = "辰"
Case 5: jyunishi = "巳"
Case 6: jyunishi = "午"
Case 7: jyunishi = "未"
Case 8: jyunishi = "申"
Case 9: jyunishi = "酉"
Case 10: jyunishi = "戌"
Case 11: jyunishi = "亥"
End Select
MsgBox "西暦 " & year & " 年の十干十二支は " & _
jikkan & jyunishi & " です。", vbInformation, "結果"
Else
MsgBox "有効な西暦を入力してください。", _
vbExclamation, "エラー"
End If
End Sub
-------------------
 
実行したら西暦を入力すれば・・・

十干十二支を表示します。