Excel精英培训网

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

[已解决]请帮忙把这个下面两个代码合并,非常感谢!

[复制链接]
发表于 2017-7-18 16:29 | 显示全部楼层 |阅读模式
请帮忙把这个下面两个代码合并,非常感谢!
代码一:
Private Sub Worksheet_Change(ByVal Target As Range)
     If Target.Count > 1 Then Exit Sub
     If Intersect(Target, [b3:b70]) Is Nothing Then Exit Sub
     Target.Offset(, 1).Resize(, 4).Validation.Delete
     Target.Offset(, 1).Resize(, 4).ClearContents
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Dim TheList As String, i&
   If Target.Count > 1 Then Exit Sub
   If Not Application.Intersect(Target, Range("A3:A500")) Is Nothing Then [a1] = Target: Exit Sub
   If Intersect(Target, [b3:e70]) Is Nothing Then Exit Sub
   Set d = CreateObject("scripting.dictionary")
   arr = Sheet2.Range("c3:f" & Sheet2.[c65536].End(3).Row)
   For i = 1 To UBound(arr)
      x = arr(i, 1)
      If Len(x) Then
        If InStr(d(x) & ",", "," & arr(i, Target.Column - 1) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 1)
      End If
   Next

   TheList = IIf(Target.Column = 2, Join(d.keys, ","), Mid(d(Cells(Target.Row, 2).Value), 2))
   With Target.Validation
      .Delete
      If Len(TheList) Then .Add xlValidateList, , , TheList
   End With
End Sub


代码二:
Private Sub Worksheet_Change(ByVal Target As Range)
     If Target.Count > 1 Then Exit Sub
     If Intersect(Target, [j3:j70]) Is Nothing Then Exit Sub
     Target.Offset(, 1).Resize(, 4).Validation.Delete
     Target.Offset(, 1).Resize(, 4).ClearContents
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
   Dim TheList As String, i&
   If Target.Count > 1 Then Exit Sub
   If Intersect(Target, [j3:m70]) Is Nothing Then Exit Sub
   Set d = CreateObject("scripting.dictionary")
   arr = Sheet5.Range("c3:f" & Sheet5.[c65536].End(3).Row)
   For i = 1 To UBound(arr)
      x = arr(i, 1)
      If Len(x) Then
        If InStr(d(x) & ",", "," & arr(i, Target.Column - 9) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 9)
      End If
   Next

   TheList = IIf(Target.Column = 10, Join(d.keys, ","), Mid(d(Cells(Target.Row, 10).Value), 2))
   With Target.Validation
      .Delete
      If Len(TheList) Then .Add xlValidateList, , , TheList
   End With
End Sub


最佳答案
2017-7-18 16:39
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.      If Target.Count > 1 Then Exit Sub
  3.      If Intersect(Target, [b3:b70]) Is Nothing Or Intersect(Target, [j3:j70]) Is Nothing Then Exit Sub
  4.      Target.Offset(, 1).Resize(, 4).Validation.Delete
  5.      Target.Offset(, 1).Resize(, 4).ClearContents
  6. End Sub

  7. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  8.    Dim TheList As String, i&
  9.    If Target.Count > 1 Then Exit Sub
  10.    If Not Application.Intersect(Target, Range("A3:A500")) Is Nothing Then [a1] = Target: Exit Sub
  11.    Set d = CreateObject("scripting.dictionary")
  12.    If Not Intersect(Target, [j3:m70]) Is Nothing Then
  13.      arr = Sheet5.Range("c3:f" & Sheet5.[c65536].End(3).Row)
  14.      For i = 1 To UBound(arr)
  15.         x = arr(i, 1)
  16.         If Len(x) Then
  17.           If InStr(d(x) & ",", "," & arr(i, Target.Column - 9) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 9)
  18.         End If
  19.      Next
  20.      TheList = IIf(Target.Column = 10, Join(d.keys, ","), Mid(d(Cells(Target.Row, 10).Value), 2))
  21.    End If
  22.    If Not Intersect(Target, [b3:e70]) Is Nothing Then
  23.      arr = Sheet2.Range("c3:f" & Sheet2.[c65536].End(3).Row)
  24.      For i = 1 To UBound(arr)
  25.         x = arr(i, 1)
  26.         If Len(x) Then
  27.           If InStr(d(x) & ",", "," & arr(i, Target.Column - 1) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 1)
  28.         End If
  29.      Next
  30.      TheList = IIf(Target.Column = 2, Join(d.keys, ","), Mid(d(Cells(Target.Row, 2).Value), 2))
  31.    End If
  32.    With Target.Validation
  33.       .Delete
  34.       If Len(TheList) Then .Add xlValidateList, , , TheList
  35.    End With
  36. End Sub
复制代码
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2017-7-18 16:39 | 显示全部楼层    本楼为最佳答案   
  1. Private Sub Worksheet_Change(ByVal Target As Range)
  2.      If Target.Count > 1 Then Exit Sub
  3.      If Intersect(Target, [b3:b70]) Is Nothing Or Intersect(Target, [j3:j70]) Is Nothing Then Exit Sub
  4.      Target.Offset(, 1).Resize(, 4).Validation.Delete
  5.      Target.Offset(, 1).Resize(, 4).ClearContents
  6. End Sub

  7. Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  8.    Dim TheList As String, i&
  9.    If Target.Count > 1 Then Exit Sub
  10.    If Not Application.Intersect(Target, Range("A3:A500")) Is Nothing Then [a1] = Target: Exit Sub
  11.    Set d = CreateObject("scripting.dictionary")
  12.    If Not Intersect(Target, [j3:m70]) Is Nothing Then
  13.      arr = Sheet5.Range("c3:f" & Sheet5.[c65536].End(3).Row)
  14.      For i = 1 To UBound(arr)
  15.         x = arr(i, 1)
  16.         If Len(x) Then
  17.           If InStr(d(x) & ",", "," & arr(i, Target.Column - 9) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 9)
  18.         End If
  19.      Next
  20.      TheList = IIf(Target.Column = 10, Join(d.keys, ","), Mid(d(Cells(Target.Row, 10).Value), 2))
  21.    End If
  22.    If Not Intersect(Target, [b3:e70]) Is Nothing Then
  23.      arr = Sheet2.Range("c3:f" & Sheet2.[c65536].End(3).Row)
  24.      For i = 1 To UBound(arr)
  25.         x = arr(i, 1)
  26.         If Len(x) Then
  27.           If InStr(d(x) & ",", "," & arr(i, Target.Column - 1) & ",") = 0 Then d(x) = d(x) & "," & arr(i, Target.Column - 1)
  28.         End If
  29.      Next
  30.      TheList = IIf(Target.Column = 2, Join(d.keys, ","), Mid(d(Cells(Target.Row, 2).Value), 2))
  31.    End If
  32.    With Target.Validation
  33.       .Delete
  34.       If Len(TheList) Then .Add xlValidateList, , , TheList
  35.    End With
  36. End Sub
复制代码

评分

参与人数 2 +31 金币 +30 收起 理由
望帝春心 + 30 + 30 来学习
cpl275538 + 1 很给力,好人一生平安,祝愿您事事顺心,一.

查看全部评分

回复

使用道具 举报

发表于 2017-7-18 16:56 | 显示全部楼层
回复

使用道具 举报

发表于 2017-7-18 17:05 | 显示全部楼层
都没有附件,瞎蒙的!
回复

使用道具 举报

 楼主| 发表于 2017-7-18 17:11 | 显示全部楼层
大灰狼1976 发表于 2017-7-18 17:05
都没有附件,瞎蒙的!

您这强大的知识可不是蒙出来,没有附件您合并好,还是能完美运行,这就是您的实力,小辈望尘莫及啊
回复

使用道具 举报

发表于 2017-7-18 17:14 | 显示全部楼层
cpl275538 发表于 2017-7-18 17:11
您这强大的知识可不是蒙出来,没有附件您合并好,还是能完美运行,这就是您的实力,小辈望尘莫及啊

谢谢了,不要过分吹捧啊,我这人就一毛病,一夸就上天!
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-24 07:04 , Processed in 0.174576 second(s), 12 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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