|
由于“发现二义性的名称 Worksheet_SelectionChange”所以要求合并这个两个代码,求大侠帮忙
代码一:鼠标点击A2至A100范围内任意单元格,则在A1显示该单元格内容。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rng As Range, rg As Range, Arr, i&, j&
Set rng = [a2:a100]
If Intersect(rng, Target) Is Nothing Then Exit Sub
[A1] = Target.Value
End Sub
代码二:在B2至B100范围内设置下拉选项,选择Sheet2表A列的内容。
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim TheList As String, i&
If Intersect(Target, [b2:b100]) Is Nothing Then Exit Sub
Set d = CreateObject("scripting.dictionary")
Arr = Sheet2.Range("a2:a" & Sheet2.[a65536].End(3).Row)
For i = 1 To UBound(Arr)
If Len(Arr(i, 1)) And InStr(TheList & ",", "," & Arr(i, 1) & ",") = 0 Then TheList = TheList & "," & Arr(i, 1)
Next
With Target.Validation
.Delete
.Add xlValidateList, , , Mid(TheList, 2)
End With
End Sub
请帮忙合并,感谢
- Private Sub Worksheet_SelectionChange(ByVal Target As Range)
- Dim TheList As String, i&
- If Not Intersect(Target, [a2:a100]) Is Nothing Then [a1] = Target.Value
- If Intersect(Target, [b2:b100]) Is Nothing Then Exit Sub
- Set d = CreateObject("scripting.dictionary")
- Arr = Sheet2.Range("a2:a" & Sheet2.[a65536].End(3).Row)
- For i = 1 To UBound(Arr)
- If Len(Arr(i, 1)) Then d(Arr(i, 1)) = ""
- Next
-
- With Target.Validation
- .Delete
- .Add xlValidateList, , , Join(d.keys, ",")
- End With
- End Sub
复制代码
用字典去重复
|
|