|
本帖最后由 quqiyuan 于 2024-12-1 11:01 编辑
代码如下。。。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count = 1 Then
Application.EnableEvents = False
If Target.Column = 10 And Target.Row > 7 Then
Set dic = CreateObject("scripting.dictionary")
With Me
r = .Cells(Rows.Count, 2).End(3).Row
brr = .[a5].Resize(r - 4, 5)
For i = 1 To UBound(brr)
If brr(i, 1) = Empty Then
brr(i, 1) = brr(i - 1, 1)
End If
Next
For i = 1 To UBound(brr)
If Not dic.exists(brr(i, 1)) Then
dic(brr(i, 1)) = brr(i, 2)
Else
dic(brr(i, 1)) = dic(brr(i, 1)) & "," & brr(i, 2)
End If
Next
With Target.Offset(, 1).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=dic(Target.Value)
.IgnoreBlank = True
.InCellDropdown = True
End With
End With
If Target.Value = Empty Then
Target.Offset(, 1).Value = Empty
Target.Offset(, 1).Validation.Delete
End If
End If
Application.EnableEvents = True
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
If InStr(Target.Address, "$B$2") Then
arr = Me.[i7].CurrentRegion
Set d = CreateObject("scripting.dictionary")
For i = 2 To UBound(arr)
If arr(i, 1) <> Empty Then
d(arr(i, 1)) = ""
End If
Next
With Me
With .Cells(2, 2).Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(d.keys, ",")
.IgnoreBlank = True
.InCellDropdown = True
End With
End With
End If
If Target.Count = 1 Then
If Target.Column = 10 And Target.Row > 7 Then
If Target.Offset(, -1) <> Empty Then
Set dic = CreateObject("scripting.dictionary")
With Me
r = .Cells(Rows.Count, 2).End(3).Row
brr = .[a5].Resize(r - 4, 5)
For i = 1 To UBound(brr)
If brr(i, 1) = Empty Then
brr(i, 1) = brr(i - 1, 1)
End If
Next
For i = 1 To UBound(brr)
If Not dic.exists(brr(i, 1)) Then
dic(brr(i, 1)) = brr(i, 2)
Else
dic(brr(i, 1)) = dic(brr(i, 1)) & "," & brr(i, 2)
End If
Next
With Target.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(dic.keys, ",")
.IgnoreBlank = True
.InCellDropdown = True
End With
End With
Else
Target.Validation.Delete
End If
End If
End If
Application.ScreenUpdating = True
End Sub
|
|