我在I8中有一个下拉框,我在I18中有一个依赖下拉框,指向I8。如果在I8中选择了apple1,那么当我点击那个盒子时,我会在I18中找到1-10果实。说我在I18选择了Fruit 9作为盒子。好吧,问题是,我决定将我的I8答案改为apple2,水果1-5将出现在I18,但6-10将不会出现。现在在I18,Fruit 9仍然被选中,我将不得不点击进行更改。
如果选择了apple1,我想选择Fruit 6-10,我想把它改成apple2,我决定将它改为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
答案 0 :(得分: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