|
本帖最后由 eess890528 于 2021-9-30 09:37 编辑
版主, 您好!
我按照您的上述提示,更改成我需要的四级数据有效性,但是提示错误(红色部分)(有时候有报错,有时候不报错),能请您帮忙看下是什么原因吗?感谢!
Private Sub Worksheet_Change(ByVal Target As Range)
Dim nL%
nL = Target.Column
If Target.Row < 7 Or Target.Row > 26 Or nL < 2 Or nL > 5 Or Target.CountLarge > 1 Then Exit Sub
Application.EnableEvents = False
Target.Offset(0, 1).Resize(1, 6 - nL).ClearContents
Application.EnableEvents = True
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim sj(), nL%, cTxt$, nRow%, arr(), ds As Object
Set ds = CreateObject("Scripting.Dictionary")
If Target.Row < 7 Or Target.Row > 26 Or Target.Column < 2 Or Target.Column > 5 Or Target.CountLarge > 1 Then Exit Sub
nL = Target.Column - 1
sj = Range("b" & Target.Row).Resize(1, 3).Value
With Sheets("基础信息")
nRow = .Cells(5000, nL).End(xlUp).Row
arr = .Range("a1").Resize(nRow, nL).Value
End With
For i = 2 To nRow
For j = 1 To nL - 1
If arr(i, j) <> sj(1, j) Then Exit For
Next
If j = nL And Not ds.exists(arr(i, nL)) Then
ds(arr(i, nL)) = ""
cTxt = cTxt & "," & arr(i, nL)
End If
Next
With Target.Validation
.Delete
If cTxt <> "" Then .Add 3, 1, 1, Mid(cTxt, 2) ’报错提示这里
End With
End Sub
|
|