|
做了两个:一个是纯函数版,不过函数有限制,所以没有用所有的数据,而且因为在数据验证中无法使用多维引用和内存数组,所以对数据源结构作了些修改。
- =OFFSET($E$2,,,COUNTA($E:$E))
- =OFFSET($B$1,MATCH($H$3,$A:$A,)-1,,COUNTIF($A:$A,$H$3))
- =OFFSET($B$1,MATCH($H$5,$A:$A,)-1,,COUNTIF($A:$A,$H$5))
复制代码
另一个是代码版,这个除了生孩子其他什么都会的代码,就没那么多顾虑了,数据源该是什么样就是什么样。
- Private Sub Worksheet_selectionChange(ByVal Target As Range)
- Dim MyRow As Long, i As Long
- Dim MyStr As String
- MyRow = Cells(Rows.Count, 1).End(xlUp).Row
- '省
- If Target.Address = "$G$4" Then
- For i = 2 To MyRow
- If Application.CountIf(Range("a1:a" & i - 1), Cells(i, 1)) = 0 Then
- MyStr = MyStr & "," & Cells(i, 1)
- End If
- Next
- MyStr = Mid(MyStr, 2, MyRow * 8)
- With Target.Validation
- .Delete
- .Add Type:=xlValidateList, Formula1:=MyStr
- End With
- End If
- '市
- If Target.Address = "$G$6" Then
- For i = 2 To MyRow
- If Cells(i, 1) = Range("g4") Then
- If Application.CountIf(Range("b1:b" & i - 1), Cells(i, 2)) = 0 Then
- MyStr = MyStr & "," & Cells(i, 2)
- End If
- End If
- Next
- MyStr = Mid(MyStr, 2, MyRow * 8)
- With Target.Validation
- .Delete
- .Add Type:=xlValidateList, Formula1:=MyStr
- End With
- End If
- '县
- If Target.Address = "$G$8" Then
- For i = 2 To MyRow
- If Cells(i, 2) = Range("g6") Then
- If Application.CountIf(Range("c1:c" & i - 1), Cells(i, 3)) = 0 Then
- MyStr = MyStr & "," & Cells(i, 3)
- End If
- End If
- Next
- MyStr = Mid(MyStr, 2, MyRow * 8)
- With Target.Validation
- .Delete
- .Add Type:=xlValidateList, Formula1:=MyStr
- End With
- End If
- End Sub
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|