Private Sub txtDateInput_LostFocus()
Dim inputValue As String
Dim formattedValue As String
Dim yearValue As Integer
Dim monthValue As Integer
inputValue = Me.txtDateInput.Value
If Len(inputValue) = 6 Then
yearValue = Val(Left(inputValue, 4))
monthValue = Val(Right(inputValue, 2))
If yearValue >= 1900 And yearValue <= 2099 And monthValue >= 1 And monthValue <= 12 Then
Me.txtDateInput.BackColor = RGB(255, 255, 255)
formattedValue = Left(inputValue, 4) & "/" & Right(inputValue, 2)
Me.txtDateInput.Value = formattedValue
Else
ShowErrorMessage()
End If
Else
ShowErrorMessage()
End If
End Sub
Private Sub ShowErrorMessage()
MsgBox "正しい形式で入力してください(YYYYMM)", vbExclamation, "エラー"
Me.txtDateInput.BackColor = RGB(255, 0, 0)
End Sub