Excel精英培训网

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

[已解决]查找组合不相同且标红

[复制链接]
发表于 2016-9-23 16:26 | 显示全部楼层 |阅读模式
本帖最后由 zwj8859 于 2016-9-23 16:29 编辑

求助:查找两列组合不相同者,且将不相同的标红。
最佳答案
2016-9-23 17:00
  1. Option Explicit

  2. Sub test1()
  3.     Dim Ends%, lstRow%, irow%, irow1%
  4.     Dim arrSrc
  5.     Dim objdic As Object
  6.     Set objdic = CreateObject("scripting.dictionary")
  7.     Ends = Cells(Rows.Count, 1).End(3).Row
  8.     lstRow = Cells(Rows.Count, 5).End(3).Row
  9.     arrSrc = Range("a2:b" & Ends).Value
  10.     For irow = 1 To UBound(arrSrc)
  11.         If Len(arrSrc(irow, 1) & arrSrc(irow, 2)) Then
  12.             If Not objdic.exists(arrSrc(irow, 1) & "," & arrSrc(irow, 2)) Then
  13.                 objdic.Add arrSrc(irow, 1) & "," & arrSrc(irow, 2), ""
  14.             End If
  15.         End If
  16.     Next
  17.     For irow1 = 2 To lstRow
  18.         If Len(Cells(irow1, 5) & Cells(irow1, 6)) Then
  19.             If Not objdic.exists(Cells(irow1, 5) & "," & Cells(irow1, 6)) Then
  20.                 Range(Cells(irow1, 5), Cells(irow1, 6)).Interior.ColorIndex = 3
  21.             End If
  22.         End If
  23.     Next
  24.     Set objdic = Nothing
  25.     Erase arrSrc
  26.     Call test2
  27. End Sub
  28. Sub test2()
  29.     Dim Ends%, lstRow%, irow%, irow1%
  30.     Dim arrSrc
  31.     Dim objdic As Object
  32.     Set objdic = CreateObject("scripting.dictionary")
  33.     Ends = Cells(Rows.Count, 5).End(3).Row
  34.     lstRow = Cells(Rows.Count, 1).End(3).Row
  35.     arrSrc = Range("E2:F" & Ends).Value
  36.     For irow = 1 To UBound(arrSrc)
  37.         If Len(arrSrc(irow, 1) & arrSrc(irow, 2)) Then
  38.             If Not objdic.exists(arrSrc(irow, 1) & "," & arrSrc(irow, 2)) Then
  39.                 objdic.Add arrSrc(irow, 1) & "," & arrSrc(irow, 2), ""
  40.             End If
  41.         End If
  42.     Next
  43.     For irow1 = 2 To lstRow
  44.         If Len(Cells(irow1, 1) & Cells(irow1, 2)) Then
  45.             If Not objdic.exists(Cells(irow1, 1) & "," & Cells(irow1, 2)) Then
  46.                 Range(Cells(irow1, 1), Cells(irow1, 2)).Interior.ColorIndex = 3
  47.             End If
  48.         End If
  49.     Next
  50.     Set objdic = Nothing
  51.     Erase arrSrc
  52. End Sub
复制代码
不同组合标红.png

VBA查找组合不同项且标红.rar

9.56 KB, 下载次数: 6

附件

发表于 2016-9-23 16:41 | 显示全部楼层
本帖最后由 Excel学徒123 于 2016-9-23 16:42 编辑

复制代码吧
  1. Option Explicit
  2. Sub test()
  3.     Dim Ends%, lstRow%, irow%, irow1%
  4.     Dim arrSrc
  5.     Dim objdic As Object
  6.     Set objdic = CreateObject("scripting.dictionary")
  7.     Ends = Cells(Rows.Count, 1).End(3).Row
  8.     lstRow = Cells(Rows.Count, 5).End(3).Row
  9.     arrSrc = Range("a2:b" & Ends).Value
  10.     For irow = 1 To UBound(arrSrc)
  11.         If Len(arrSrc(irow, 1) & arrSrc(irow, 2)) Then
  12.             If Not objdic.exists(arrSrc(irow, 1) & "," & arrSrc(irow, 2)) Then
  13.                 objdic.Add arrSrc(irow, 1) & "," & arrSrc(irow, 2), ""
  14.             End If
  15.         End If
  16.     Next
  17.     For irow1 = 2 To lstRow
  18.         If Len(Cells(irow1, 5) & Cells(irow1, 6)) Then
  19.             If Not objdic.exists(Cells(irow1, 5) & "," & Cells(irow1, 6)) Then
  20.                 Range(Cells(irow1, 5), Cells(irow1, 6)).Interior.ColorIndex = 3
  21.             End If
  22.         End If
  23.     Next
  24.     Set objdic = Nothing
  25.     Erase arrSrc
  26. End Sub
复制代码


回复

使用道具 举报

 楼主| 发表于 2016-9-23 16:48 | 显示全部楼层
本帖最后由 zwj8859 于 2016-9-23 16:49 编辑

A&B组合列不同者,没有标红。E&F列已经有标红了。
回复

使用道具 举报

发表于 2016-9-23 16:56 | 显示全部楼层
zwj8859 发表于 2016-9-23 16:48
A&B组合列不同者,没有标红。E&F列已经有标红了。

还要标AB列????
回复

使用道具 举报

发表于 2016-9-23 17:00 | 显示全部楼层    本楼为最佳答案   
  1. Option Explicit

  2. Sub test1()
  3.     Dim Ends%, lstRow%, irow%, irow1%
  4.     Dim arrSrc
  5.     Dim objdic As Object
  6.     Set objdic = CreateObject("scripting.dictionary")
  7.     Ends = Cells(Rows.Count, 1).End(3).Row
  8.     lstRow = Cells(Rows.Count, 5).End(3).Row
  9.     arrSrc = Range("a2:b" & Ends).Value
  10.     For irow = 1 To UBound(arrSrc)
  11.         If Len(arrSrc(irow, 1) & arrSrc(irow, 2)) Then
  12.             If Not objdic.exists(arrSrc(irow, 1) & "," & arrSrc(irow, 2)) Then
  13.                 objdic.Add arrSrc(irow, 1) & "," & arrSrc(irow, 2), ""
  14.             End If
  15.         End If
  16.     Next
  17.     For irow1 = 2 To lstRow
  18.         If Len(Cells(irow1, 5) & Cells(irow1, 6)) Then
  19.             If Not objdic.exists(Cells(irow1, 5) & "," & Cells(irow1, 6)) Then
  20.                 Range(Cells(irow1, 5), Cells(irow1, 6)).Interior.ColorIndex = 3
  21.             End If
  22.         End If
  23.     Next
  24.     Set objdic = Nothing
  25.     Erase arrSrc
  26.     Call test2
  27. End Sub
  28. Sub test2()
  29.     Dim Ends%, lstRow%, irow%, irow1%
  30.     Dim arrSrc
  31.     Dim objdic As Object
  32.     Set objdic = CreateObject("scripting.dictionary")
  33.     Ends = Cells(Rows.Count, 5).End(3).Row
  34.     lstRow = Cells(Rows.Count, 1).End(3).Row
  35.     arrSrc = Range("E2:F" & Ends).Value
  36.     For irow = 1 To UBound(arrSrc)
  37.         If Len(arrSrc(irow, 1) & arrSrc(irow, 2)) Then
  38.             If Not objdic.exists(arrSrc(irow, 1) & "," & arrSrc(irow, 2)) Then
  39.                 objdic.Add arrSrc(irow, 1) & "," & arrSrc(irow, 2), ""
  40.             End If
  41.         End If
  42.     Next
  43.     For irow1 = 2 To lstRow
  44.         If Len(Cells(irow1, 1) & Cells(irow1, 2)) Then
  45.             If Not objdic.exists(Cells(irow1, 1) & "," & Cells(irow1, 2)) Then
  46.                 Range(Cells(irow1, 1), Cells(irow1, 2)).Interior.ColorIndex = 3
  47.             End If
  48.         End If
  49.     Next
  50.     Set objdic = Nothing
  51.     Erase arrSrc
  52. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2016-9-23 17:01 | 显示全部楼层
Excel学徒123 发表于 2016-9-23 16:56
还要标AB列????

A&B列,还有不同的,所以要标红。麻烦你了!
回复

使用道具 举报

发表于 2016-9-23 17:02 | 显示全部楼层
zwj8859 发表于 2016-9-23 17:01
A&B列,还有不同的,所以要标红。麻烦你了!

自己看5楼答案
回复

使用道具 举报

 楼主| 发表于 2016-9-23 17:36 | 显示全部楼层
代码随长但功能还是可用的。谢谢!
回复

使用道具 举报

 楼主| 发表于 2016-9-24 18:39 | 显示全部楼层

高手老师:能否再帮一个忙,把标色的组合单元格分别粘帖至右边位置。具体见附件。

VBA查找组合不同项且标红.rar

26.96 KB, 下载次数: 4

回复

使用道具 举报

发表于 2016-9-24 18:57 | 显示全部楼层
zwj8859 发表于 2016-9-24 18:39
高手老师:能否再帮一个忙,把标色的组合单元格分别粘帖至右边位置。具体见附件。

自己下吧

VBA查找组合不同项且标红.rar

36.11 KB, 下载次数: 2

回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-19 21:23 , Processed in 0.269679 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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