2017-03-15 5 views
2

以前のユーザー入力に基づいてユーザーエントリを制限するExcelでいくつかのデータ検証を実行しようとしています。私はこれをワークシートの変更に基づいて更新したいと思います。コードは次のとおりです。ワークシートの変更によるデータ検証

Private Sub Worksheet_Change(ByVal Target As Range) 
'If Target.Cells.Count > 1 Then Exit Sub 
'Checks if cell is Record or Assumption and applies appropriate data validation 
Dim Cell As Range 
Application.EnableEvents = False 
    If Target.Column = 59 Or 72 Or 85 Or 98 Or 111 Then 'Refers to the columns that require Rec/Ass 
     Set Cell = Target.Offset(0, 3) 
'MsgBox Cell & " " & Target.Value 'USED TO ERROR CHECK 
     If Target.Value = "N/A" Then 
      Cell.Validation.Delete 
      Cell.Value = vbNullString 
      Cell.Offset(0, 1).Validation.Delete 
      Cell.Offset(0, 1).Value = vbNullString 
     Else 
      If Target.Value = "Record" Then 
       With Cell.Validation 
        .Delete 
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Record" 
       End With 
      ElseIf Target.Value = "Assumption" Then 
       Cell.Validation.Delete 
       Cell.Value = vbNullString 
       Cell.Offset(0, 1).Value = vbNullString 
      End If 
     End If 
     Set Cell = ActiveCell 
    End If 

    If Target.Column = 62 Or 75 Or 88 Or 101 Or 114 Then 'Refers to the columns that require Doc Type 
     Desig = Application.VLookup(Target.Value, Sheets("Record Source Chart").Range("RecordDesig"), 21, False) 
     If Target.Value = "" Then Exit Sub 
     Else 
      Set Cell = Target.Offset(0, 1) 
      With Cell.Validation 
       .Delete 
       .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=" & Desig 
      End With 
    End If 
Application.EnableEvents = True 
End Sub 

2番目のif文に問題があります。このコードを実行すると、2番目のdata.validationは追加されません。これは、更新するまでセルが空白になっていると思いますが、更新しても検証は行われません。また、Target.Column = 59でもIf Target.Value = "" Then Exit Subを削除すると、2番目のif文が実行され、 "型の不一致"エラーが返されます。どんな助けでも大歓迎です、ありがとうございます。

答えて

0

変更この:

If Target.Column = 62 Or 75 Or 88 Or 101 Or 114 Then 

へ:いっそ

If Target.Column = 62 Or Target.Column = 75 Or Target.Column = 88 Or Target.Column = 101 Or Target.Column = 114 Then 

あるいは、変更Select Caseへ:

Select Case Target.Column 
    Case 62, 75, 88, 101, 114 
     ' rest of your code goes here... 

End Select 

:あなたも、それを変更する必要があります最初の時間:

If Target.Column = 59 Or 72 Or 85 Or 98 Or 111 Then 'Refers to the columns that require Rec/Ass 
+0

私は完全に "ケース"句について知りませんでした。助けてくれてありがとう、私はそれを解決したと思う。問題は、私は常に "Target.Column"をオフセットによって変更していたことでした。あなたの最初の解決策はうまくいかなかった、私が直面していたのと同じ問題を引き起こした。 「ケースの選択」が修正されたようですが、もう一度お試しください! –

関連する問題