2016-04-22 12 views
-1

いくつかのテキストボックスを持つユーザーフォームがあり、フォームの最後のコマンドボタンをクリックするとデータを検証する必要があります。コードは次のとおりです。エラーメッセージの後にフォーカスをテキストボックスに戻す方法。

Private Sub CmdSave1_Click() 

Dim row As Long 
    Dim c As Range 
row = ActiveCell.row 


    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).row) 
    If c.Value = txt_BPName1 Then 
     MsgBox " Duplicate Found.Please enter unique Base Product" 
     txt_BPName1.SetFocus '>>> the cursor does not return textbox here. 
       'txt_SPName1.SetFocus 
       End If 
       Next 
       'txt_SPName1.SetFocus 
       'Exit Sub 
    For Each c In Range("B2:B" & Cells(Rows.Count, 1).End(xlUp).row) 
     If c.Value = txt_SPName1 Then 
      MsgBox "Cell " & c.Address & " Duplicate sub Product Found." 
       txt_SPName1.SetFocus 
       End If 
       Next 
       'txt_loc1.SetFocus 
       'Exit Sub 

exit subは、カーソルをテキストボックスに戻します。しかし、私はサブラインを終了するより下で発生する必要がある他のコード行を持っています。だから、私はサブを終了したくない。 exit subの代わりになりますか?または、私は壊れて、再びサブバックに入ることができますか?

+0

VB.NETではない、それはVBScriptのではなく、それはマクロではない、VBAはありません。これらのタグはすべて無関係です。 VB6コード – Plutonix

+0

"Exit Sub"はカーソルをテキストボックスに置かず、 "txt_SPName1.SetFocus"はテキストボックスにカーソルを置きます。 Exit Subはコードの実行を停止します。ちなみに、このコードは、各レコードをループして検索する代わりに、find関数を使用してすでに重複があるかどうかを確認すると、はるかに明確で効率的になります。私はそれを研究と実装のために残しておきます。 – OpiesDad

+0

@Plutonix私はそれがVBAだと思いますが、正しいですが、ほとんどのタグは無関係です。 – OpiesDad

答えて

0

あなたは「後藤」ステートメントを使用することができます

For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).row) 
     If c.Value = txt_BPName1 Then 
     MsgBox " Duplicate Found.Please enter unique Base Product" 
     txt_BPName1.SetFocus '>>> the cursor does not return textbox here. 
     'txt_SPName1.SetFocus 
     GoTo DuplicateFound 
     End If 
Next 

....rest of code 

DuplicateFound: 
     ....code you want to happen if a duplicate is found. 
関連する問題