Private Sub 郵便番号_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim myStr As String
myStr = 郵便番号()
If (Len(myStr) = 3) And (KeyCode <> 8) Then
郵便番号.Text = myStr & "-" Else
If (Len(myStr) = 4) And (KeyCode <> 8) Then
郵便番号.Text = Left(myStr, 3) & "-" & Right(myStr, 1) End If
Private Sub 郵便番号_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim myStr As String
myStr = 郵便番号()
If (Len(myStr) = 3) And (KeyCode <> 8) Then
郵便番号.Text = myStr & "-"
ElseIf (Len(myStr) = 4) And (KeyCode <> 8) Then
郵便番号.Text = Left(myStr, 3) & "-" & Right(myStr, 1)
End If
'**********8文字オーバーした場合は,9文字以降切り捨て
If Len(myStr) > 8 Then
MsgBox "郵便番号は8文字以下で入力しましょう"
郵便番号.Text = Left(myStr, 8)
End If
End Sub
'************半角の数字のみを受け付ける************************
Private Sub 郵便番号_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii < Asc("0") Or KeyAscii > Asc("9") Then
Beep
KeyAscii = 0
End If
End Sub
Private Sub 郵便番号_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim myStr As String
myStr = 郵便番号()
If (Len(myStr) = 3) And (KeyCode <> 8) Then
郵便番号.Text = myStr & "-" '郵便番号 XXX-XXXX の"-"自動入力
Else
If (Len(myStr) > 3) And (Mid(myStr, 4, 1) <> "-") And (Mid(myStr, 5, 1) <> "-") Then
郵便番号.Text = Left(myStr, 3) & "-" & Mid(myStr, 4, 4)
End If
If (Mid(myStr, 3, 1) = "-") And (Mid(myStr, 4, 1) <> "-") Then
郵便番号.Text = Left(myStr, 2) & Mid(myStr, 4, 1) & "-" & Mid(myStr, 5, 3)
End If
If (Mid(myStr, 5, 1) = "-") Then
郵便番号.Text = Left(myStr, 3) & "-" & Mid(myStr, 4, 1) & Mid(myStr, 6, 3)
End If
End If
End Sub