Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 4585|回复: 2

[已解决]发现二义性的名称 Worksheet_SelectionChange 求把两个代码合并

[复制链接]
发表于 2017-7-15 13:06 | 显示全部楼层 |阅读模式
由于“发现二义性的名称 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


请帮忙合并,感谢

最佳答案
2017-7-15 15:08
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.    Dim TheList As String, i&
  3.    If Not Intersect(Target, [a2:a100]) Is Nothing Then [a1] = Target.Value
  4.    If Intersect(Target, [b2:b100]) Is Nothing Then Exit Sub
  5.    Set d = CreateObject("scripting.dictionary")
  6.    Arr = Sheet2.Range("a2:a" & Sheet2.[a65536].End(3).Row)
  7.    For i = 1 To UBound(Arr)
  8.       If Len(Arr(i, 1)) Then d(Arr(i, 1)) = ""
  9.    Next
  10.    
  11.    With Target.Validation
  12.       .Delete
  13.       .Add xlValidateList, , , Join(d.keys, ",")
  14.    End With
  15. End Sub
复制代码

用字典去重复

Book2.rar

11.05 KB, 下载次数: 9

发表于 2017-7-15 15:03 | 显示全部楼层
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)) 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
回复

使用道具 举报

发表于 2017-7-15 15:08 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  2.    Dim TheList As String, i&
  3.    If Not Intersect(Target, [a2:a100]) Is Nothing Then [a1] = Target.Value
  4.    If Intersect(Target, [b2:b100]) Is Nothing Then Exit Sub
  5.    Set d = CreateObject("scripting.dictionary")
  6.    Arr = Sheet2.Range("a2:a" & Sheet2.[a65536].End(3).Row)
  7.    For i = 1 To UBound(Arr)
  8.       If Len(Arr(i, 1)) Then d(Arr(i, 1)) = ""
  9.    Next
  10.    
  11.    With Target.Validation
  12.       .Delete
  13.       .Add xlValidateList, , , Join(d.keys, ",")
  14.    End With
  15. End Sub
复制代码

用字典去重复

评分

参与人数 1 +1 收起 理由
cpl275538 + 1 很给力,感谢

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 16:29 , Processed in 0.338781 second(s), 14 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表