2016-08-09 3 views
1

次のコードは、列内の特定のテキスト文字列を検索し、一致するとメッセージボックスを表示します。コードは複数のテキスト文字列を検索します。したがって、1つの列に "X"と "y"があり、コードが両方のテキスト文字列を検索すると、2つのmsgボックスが表示されます。私は最初のmsgボックスが残りを表示して隠すだけです。これを行う方法はありますか?Excel VBAでポップアップする他のメッセージボックスを非表示にする

つまり、コードは複数のテキスト文字列を検索し、テキスト文字列が一致するとmsgボックスをポップアップします。複数のテキスト文字列が確実に一致しますが、最初のボックスのみを表示し、残りの文字列を非表示にします。

おかげ

Private Sub Worksheet_Change(ByVal Target As Range) 

Dim icounter As Long 
Dim icounter1 As Long 
Dim lastrow As Long 

Dim MSG As String, ans As Variant 
For icounter = 2 To 31 

    If Cells(icounter, 2) = "Job Code Name" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

ElseIf Cells(icounter, 2) = "Personnel Area" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

ElseIf Cells(icounter, 2) = "Line of Sight" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

ElseIf Cells(icounter, 2) = "Title of Position" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

ElseIf Cells(icounter, 2) = "Company Code Name" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

ElseIf Cells(icounter, 2) = "Function" Then 
     MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 

Else 

End If 

Next icounter 
End Sub 
+1

をしましたあなたが何をしっかりしているのか分かりませんが、Exit Forでループを終了することができます。 – Comintern

+1

あなたがしたいところで 'exit sub'か' exit for'だけです。すべての 'MsgBox'コールの後にあなたのケースで – lokusking

+0

ループを最初の一致の後に停止するようにコードを編集するには?ありがとう! – Daruki

答えて

3

私はあなたの質問を理解する場合は、代わりに、すべての場合は...のElseIfものの選択ケースを使用することができます。コメントを読むだけです。明らかにForループを終了したいので、Exit Forを追加してください。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim icounter As Long 
    Dim icounter1 As Long 
    Dim lastrow As Long 

    Dim MSG As String, ans As Variant 
    For icounter = 2 To 31 
     Select Case Cells(icounter, 2) 
      Case "Job Code Name" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
      Case "Personnel Area" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
      Case "Line of Sight" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
      Case "Title of Position" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
      Case "Company Code Name" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
      Case "Function" 
       MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
       Exit For 
     End Select 
    Next icounter 
End Sub 
+0

ありがとうございます!上記のもの以外のテキスト文字列を入力すると、メッセージボックスがポップアップし、前のメッセージボックスに対応するmsgボックスが表示されます。私。 - >「人事エリア」の行と「blahblah」の行がある場合は、「人事エリア」のmsgポップアップが表示されます。とにかくこれを避けるためにそこにいますか? – Daruki

+0

また、他の列の変更が依然としてmsgボックスに表示されることに気づいています。 – Daruki

+0

私はあなたの答えを使用しましたが、シート内の任意のセルを編集するたびに発生しました。これを避けるために、 'Select Case Cells(icounter、2)'の前に次のコード行を追加しました: 交差していない場合(ターゲット、セル(icounter、2))は何もありません この編集でmsgボックスセルが入力した文字列のテキスト、つまり会社コード名に変更されたときにのみポップアップ – Daruki

0

コードで別のリファクタリングをお勧めします。ワークシートイベントを使用しているので、セルコンテンツを変更するたびにそのイベントが発生しますので、Forループの前にApplication.EnableEvents = Falseを追加し、その後にApplication.EnableEvents = Trueを追加すると思います。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim icounter As Long 
    Dim icounter1 As Long 
    Dim lastrow As Long 
Dim MSG As String, ans As Variant 

Application.EnableEvents = False 
For icounter = 2 To 31 
    Select Case Cells(icounter, 2) 
     Case "Job Code Name" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. PS Group" & vbNewLine & "2. Level" & vbNewLine & "3. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
     Case "Personnel Area" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Personnel Subarea" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
     Case "Line of Sight" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. 1 Up Line of Sight" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
     Case "Title of Position" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Job Code Name" & vbNewLine & "2. PS Group" & vbNewLine & "3. PS Level" & vbNewLine & "4. Box Level" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
     Case "Company Code Name" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Cost Center" & vbNewLine & "2. Line of Sight" & vbNewLine & "3. 1 Up Line of Sight" & vbNewLine & "4. Personnel Area" & vbNewLine & "5. Location" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
     Case "Function" 
      MsgBox ("Please note you may need to add in additional attributes under this field" & vbNewLine & vbNewLine & "1. Sub Function" & vbNewLine & vbNewLine & "Please add in these additional fields as needed") 
      Exit For 
    End Select 
Next icounter 

Application.EnableEvents = False 
End Sub 
0

私はティムの答えを使用しましたが、シート内の任意のセルを編集するたびに発生しました。これを避けるために、私はSelect Case Cells(icounter, 2)前のコード行を追加:

If Not Intersect(Target, Cells(icounter, 2)) Is Nothing Then 

この編集は、細胞は、私は、入力された文字列のテキストに変更する場合にのみ、MSGボックスがポップアップすなわち、会社コードネーム私は

関連する問題