2016-06-18 20 views
1

ユーザーは、はい/いいえを列Cに挿入する必要があります。いいえの場合、次のセルにはN/Aと表示され、灰色で表示されます。はいの場合、次のセルは黄色で強調表示され、ユーザーはそのセルに記入することができます。VBA - セルの変更

コードは以下のとおりですが、セルにnoが設定されてからyesに変更された場合、次のセルはN/Aからハイライトに変更されません。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim Cell As Range 
    If Target.Column = 3 Then 
     Set Cell = Target.Offset(0, 1) 
     If Len(Target.Value) = 0 Then 
      Cell.Validation.Delete 
      Cell.Value = vbNullString 
     Else 
      If Target.Value = "Yes" Then 
       With Cell.Validation 
        Cell.Interior.ColorIndex = 36 
       End With 
      ElseIf Target.Value = "No" Then 
       Cell.Validation.Delete 
       Cell.Value = "N/A" 
      Else 
       MsgBox "Input only Yes or No." 
       Target.ClearContents 
       Cell.Validation.Delete 
      End If 
     End If 
    End If 
    End Sub 

答えて

1

これは、yes条件のifブロックに行を追加して隣接セルの値を変更しなかったためです。また、noブロックの場合は、セルの色を白に戻したい場合があります。そうでない場合は、前にイエスになってから黄色になります。以下のコードはあなたが望むものを達成するはずです。

Private Sub Worksheet_Change(ByVal Target As Range) 
    Dim Cell As Range 
    If Target.Column = 3 Then 
     Set Cell = Target.Offset(0, 1) 
     If Len(Target.Value) = 0 Then 
      Cell.Validation.Delete 
      Cell.Value = vbNullString 
     Else 
      If Target.Value = "Yes" Then 
       With Cell.Validation 
        Cell.Interior.ColorIndex = 36 
       End With 
       Cell.Value = "" 
      ElseIf Target.Value = "No" Then 
       Cell.Validation.Delete 
       Cell.Value = "N/A" 
       Cell.Interior.ColorIndex = 0 
      Else 
       MsgBox "Input only Yes or No." 
       Target.ClearContents 
       Cell.Validation.Delete 
      End If 
     End If 
    End If 
    End Sub 
0

あなたは、セルの内容をクリアしながら、あなたはEnableEventsをオンにする必要があります

If target.value = "yes" then 
    cell.validation.delete 
    cell.interior.color = vbyellow 
    cell.clearcontents 
end if 
0
  • を試してみてください。 Worksheet_Changeが再び起動されないようにします。それ以外の場合は、ループで立ち往生します。
  • Excelの組み込みセル検証で検証を行っていません
  • Excelの組み込みセル検証と条件付き書式を使用して、実行する必要があります。

    • このメソッドは、1つ以上のセルのセル値に影響を与える場合には失敗し、セルの検証と条件付きの書式設定は正常に機能します。レンジ

      として

    ます。Private Sub Worksheet_Change(ByValの対象を範囲) 薄暗いセル

    If Target.Column = 3 Then 
        Application.EnableEvents = False 
        Set Cell = Target.Offset(0, 1) 
        With Cell.Interior 
         If Target.Value = "Yes" Then 
          'Change Cell Color to Yellow 
          .ColorIndex = 36 
         ElseIf Target.Value = "No" Then 
          'Change Cell Color to Grey 
          ' Insert Grey Color Change 
          Cell.Value = "N/A" 
         Else 
          Target.ClearContents 
          Cell.ClearContents 
          With Cell.Interior 
           .Pattern = xlNone 
           .TintAndShade = 0 
           .PatternTintAndShade = 0 
          End With 
          MsgBox "Input only Yes or No." 
         End If 
        End With 
        Application.EnableEvents = True 
    End If 
    

    End Subの

、彼らが価値になりたい場合は、依頼するメッセージボックスを使用することができますはい、もしくは、いいえ。

iResponse = MsgBox("Input only Yes or No.", vbYesNoCancel) 
Select Case iResponse 
    Case vbYes 
     Target.Value = "Yes" 
    Case vbNo 
     Target.Value = "Yes" 
    Case Else 
     'You need to turn of Events when clearing the cell 
     'So that the Worksheet_Change won't fire again 
     Application.EnableEvents = False 
     Target.ClearContents 
     Cell.ClearContents 

End Select 
関連する問題