2017-03-29 15 views
1

I8にドロップダウンボックスがあり、I18にI8に向かう依存ドロップダウンボックスがあります。おそらくapple1がI8で選択されていれば、そのボックスをクリックするとI18に列挙された果実1-10が得られます。私がI18のボックスにFruit 9を選んだとします。まあ、問題は、私はapple2に私のI8の答えを変更することを決めたと言う、果物1-5はI18に表示されますが、6-10は表示されません。今I18では、Fruit 9がまだ選択されています。変更するには、これをクリックする必要があります。VBAの従属ドロップダウンボックスに基づいてセルをクリアする

apple1が選択されている間にFruits 6-10を選択した場合、apple2に変更することに決めた場合、私のI18は空白になります。

Private Sub Worksheet_Change(ByVal Target As Range) 
On Error Resume Next 
If Target.Address = "$I$8" Then 
    If Target.Address = "6" Or "7" Or "8" Or "9" Or "10" Then 
    If Target.Validation.Type = 3 Then 
    Application.EnableEvents = False 
    Target.Offset(10, 0).ClearContents 
    End If 
    End If 
End If 

exitHandler: 
Application.EnableEvents = True 
Exit Sub 
End Sub 

答えて

2

いくつかの問題:

は、ここに私のコードです。リファクタリングされたコードの注記を参照してください。

Private Sub Worksheet_Change(ByVal Target As Range) 

'On Error Resume Next ->> delete this, it will not allow you to see real errors in your code 

If Target.Address = "$I$8" Then 

    'you ask it define Target.Address = 6, etc. Wrong property. You need Target.Value (
    'think about it, you just checked for Address = $I$8, so how can it also be 6 - 10 
    'also if you do want to use IF, syntax is Target.Value = 6 or Target.Value = 7 ... not 6 or 7 or ... 

    Select Case Target.value 'used this as it more efficient 

      Case 6 to 10 

      If Target.Validation.Type = 3 Then 
       Application.EnableEvents = False 
       Target.Offset(10, 0).ClearContents 
      End If 

    End Select 

End If 

exitHandler: 
Application.EnableEvents = True 
Exit Sub 
End Sub 
+0

@ScottHoltzmanに変更を加えました。しかし、私のI18値は、大文字と小文字の区別なく、常にクリアされます。提案? – Derrick

+0

*(+ 1)*私はあなたのアプローチが特に好きです。通常、 'If Target.Value> = 6、Target.Value <= 10 Then'でチェックします。しかし、これは 'Target.Value'が文字列か' Empty'かエラー値である可能性を考慮に入れていません。 'Case .... Select'では気にしないで' Target.Value'が実際に 'If'の前に' IsNumeric() 'を実際に使用しているかどうかを確認する必要はありません。よりクリーンで、より簡単でより直接的な方法です。 – Ralph

+0

@Derrick - 行動をテストするために行ごとにコードをステップバイステップで実行しましたか? –

関連する問題