|
楼主 |
发表于 2013-12-8 12:52
|
显示全部楼层
zhaogang1960 发表于 2013-12-8 00:19
请注意:此代码仅适应每次输入一个单元格
请看附件
赵老师威武~但是您写的我看的不是很明白,不是太会改~
开始的时候我是用您很早之前的写的去重复项的一段,见下,红字部分改好了就好了,麻烦您看一下,谢谢~
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Row > 1 And .Column < 7 And .Column > 4 Then
For i = 1 To .Rows.Count
'初始化 部室、班组 下拉菜单
If Len(Trim(Cells(.Row + i - 1, 5))) > 0 Then
er = Sheet1.Cells(Rows.Count, 2).End(xlUp).Row
If er = 1 Then er = 2
'BuShiArr = Sheet1.Range("b2:b" & er)
BuShiArr = Bcfz(Sheet1.Range("b2:b" & er))
BuShiStr = Join(BuShiArr, ",")
Cells(.Row + i - 1, 6).Locked = False
Cells(.Row + i - 1, 6).Validation.Delete
Cells(.Row + i - 1, 6).Validation.Add xlValidateList, Formula1:=BuShiStr
Cells(.Row + i - 1, 6).Validation.ShowError = False
BanZuArr = Bcfz(Sheet1.Range("b2:c" & er)) '这个地方不会写,求指点
BanZuStr = Join(BuShiArr, ",")
Cells(.Row + i - 1, 7).Locked = False
Cells(.Row + i - 1, 7).Validation.Delete
Cells(.Row + i - 1, 7).Validation.Add xlValidateList, Formula1:=BanZuStr
Cells(.Row + i - 1, 7).Validation.ShowError = False
Else
Cells(.Row + i - 1, 6).Validation.Delete
Cells(.Row + i - 1, 7).Validation.Delete
End If
Next
End If
End With
End Sub
Function Bcfz(rng As Range)
Dim d As Object, rCell As Range
Set d = CreateObject("Scripting.Dictionary")
On Error Resume Next
For Each rCell In rng
If Not d.exists(rCell.Text) Then
If rCell <> "" Then
d.Add rCell.Text, 1
End If
End If
Next rCell
Bcfz = d.keys
Set d = Nothing
' 函数用法
' Sub yy1()
' Dim rng As Range ‘声明变量rng为区域对象
' Set rng = [a1:c10] ‘把A1到C10单元格区域赋值给变量rng
' [d1].Resize(UBound(Bcfz(rng)) + 1, 1) = Application.Transpose(Bcfz(rng))
' End Sub
End Function
下拉菜单V2.rar
(13.72 KB, 下载次数: 34)
|
|