プロが教える店舗&オフィスのセキュリティ対策術

ユーザーフォームからの入力で下記のコードなんですが・・・

色々とこちらで質問して教えて頂いたものを継ぎ足し変更しで何とか作りました。
ですが、二重登録防止の所で「No」を選択した場合に処理を中断は出来るのですが、入力データがそのまま残ってしまいます。
処理を中断した場合に入力データをクリアしてTextBox1にセットフォーカスし、新たに入力を開始するには何が足りませんか?訂正箇所等ありますか?
特にエラーにならないので自分では探せずに困っています。
詳しい方、お教え下さい。

Dim Ar As Variant
Private cmbFlg As Boolean
Option Explicit
Private Sub UserForm_Initialize()

'オプションボタンの内容の設定
Ar = Array("外注", "仕入", "未払")
End Sub
Private Sub CommandButton1_Click()

Dim Ctl As Control, rng As Range, i As Long

With Worksheets("sheet1")

'二重登録の防止
Dim rc As VbMsgBoxResult
rc = MsgBox("次の入力をしますか?", vbYesNo + vbQuestion)
If rc = vbYes Then
Else
MsgBox "処理を中止します", vbCritical

Exit Sub

End If

Set rng = Range("A" & Rows.Count).End(xlUp).Offset(1).Cells


rng.Value = Me.TextBox1.Text
rng.Offset(, 1) = Me.TextBox2.Text
rng.Offset(, 2) = Me.TextBox3.Text

If OptionButton1 = True Then
rng.Offset(, 3).Value = OptionButton1.Caption
ElseIf OptionButton2 = True Then
rng.Offset(, 3).Value = OptionButton2.Caption
ElseIf OptionButton3 = True Then
rng.Offset(, 3).Value = OptionButton3.Caption

End If

rng.Offset(, 4) = Me.TextBox4.Text
rng.Offset(, 5) = Me.TextBox5.Text

For Each Ctl In Controls

If TypeName(Ctl) = "TextBox" Then

Ctl.Value = ""

End If

UserForm1.TextBox1.SetFocus

Next Ctl
End With
End Sub

Private Sub OptionButton1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
'テンキー入力: 1外注, 2仕入,3未払
Dim flg As Boolean
Dim i As Long, j As Long
If ActiveControl.Name <> "Frame1" Then Exit Sub
If KeyCode = 97 Then OptionButton1.Value = True: j = 1
If KeyCode = 98 Then OptionButton2.Value = True: j = 2
If KeyCode = 99 Then OptionButton3.Value = True: j = 3

If KeyCode = 13 And flg Then TextBox4.SetFocus
End Sub

Private Sub optionbutton1_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)

'Enterで次のフォーカスに移動
If KeyCode = vbKeyReturn Then
Me.TextBox4.SetFocus
End If
End Sub

Private Sub CommandButton2_Click()

'終了ボタン
If MsgBox("終了しますか?", vbQuestion + vbOKCancel) = vbOK Then
Unload Me
End If

End Sub

A 回答 (1件)

前の質問の流れも今回のコードも全部精読していないので間違ってたら恐縮ですが、以下ではどうでしょうか。


Else節に追記しています。

---------------------------------------------------------------------------

'二重登録の防止
Dim rc As VbMsgBoxResult
rc = MsgBox("次の入力をしますか?", vbYesNo + vbQuestion)
If rc = vbYes Then

Else
   MsgBox "処理を中止します", vbCritical
      
   For k=1 To 5
     Controls("TextBox" & k).Value=""
   Next k

   TextBox1.SetFocus

   Exit Sub

End If
    • good
    • 0
この回答へのお礼

有難うございました。思う通りの動作になりました。
自分のコードとの違いもわかりました。

お礼日時:2015/06/04 08:13

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!