|
操作表2代码增加了一个 Range 变量 ran2 ,缩小取值范围,修改后,代码有效了,如下:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
If Target.Column = 1 Then Target.Offset(0, 1).Resize(1, 2).ClearContents
If Target.Column = 2 Then Target.Offset(0, 1).ClearContents
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column > 3 Or Target.Row = 1 Then Exit Sub
On Error Resume Next
Dim col As New Collection, ran As Range, c As Byte, val As String
If Target.Column = 1 Then
For Each ran In Sheets(1).Range("e2:e9")
col.Add ran, key:=CStr(ran)
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 2 Then
For Each ran In Sheets(1).Range("f2:f9")
If ran.Offset(0, -1) = Target.Offset(0, -1) Then
col.Add ran ', key:=CStr(ran)
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
ElseIf Target.Column = 3 Then
For Each ran In Sheets(1).Range("g2:k9")
If ran.Offset(0, -1) = Target.Offset(0, -1) And ran.Offset(0, -2) = Target.Offset(0, -2) Then
Dim ran2 As Range
For Each ran2 In ran.Resize(1, 5)
col.Add ran2 ', key:=CStr(ran)
Next
End If
Next
For c = 1 To col.Count
val = val & col(c) & ","
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, Formula1:=val
End With
End If
End Sub
|
|