Excel精英培训网

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

[已解决]VBA 代码求助

[复制链接]
发表于 2012-10-10 15:40 | 显示全部楼层 |阅读模式
问题描述:现有左右两列(F列、G列)数据,想通过代码实现,左右两列数据相等的行,用颜色标记出来。例如单元格F2=G256,则第2行与256行均用绿色标记;F4=G95,则第4行与95行均用绿色标记。  
最佳答案
2012-10-10 16:37
  1. Sub 比较2()
  2.     Dim dic1 As Object, dic2 As Object
  3.     Dim iRow As Long
  4.     Dim i As Long, arr, srg As String, str1 As String, str2 As String
  5.     iRow = [f1].End(xlDown).Row
  6.     arr = Range("f1:g" & iRow).Value
  7.     Set dic1 = CreateObject("scripting.dictionary")
  8.     Set dic2 = CreateObject("scripting.dictionary")
  9.    
  10.     For i = 2 To iRow
  11.         If Not dic1.exists(arr(i, 1)) And arr(i, 1) <> 0 Then
  12.             dic1.Add arr(i, 1), i
  13.         End If
  14.         If Not dic2.exists(arr(i, 2)) And arr(i, 2) <> 0 Then
  15.             dic2.Add arr(i, 2), i
  16.         End If
  17.     Next
  18.     arr = dic1.keys
  19.     For i = 0 To UBound(arr)
  20.         If dic1.exists(arr(i)) And dic2.exists(arr(i)) Then
  21.             str1 = Replace(Cells(Val(dic1(arr(i))), "f").Address, "$", "") & ","
  22.             str2 = Replace(Cells(Val(dic2(arr(i))), "f").Address, "$", "") & ","
  23.             If Len(srg & str1 & str2) > 249 Then
  24.                 Range(Left(srg, Len(srg) - 1)).EntireRow.Interior.ColorIndex = 35
  25.                 srg = str1 & str2
  26.             Else
  27.                 srg = srg & str1 & str2
  28.             End If
  29.         End If
  30.     Next
  31. End Sub
复制代码

判断.rar

11.42 KB, 下载次数: 11

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2012-10-10 16:04 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-10-10 16:09 编辑

如左右列的数据重复值,是不会变色的对不?
回复

使用道具 举报

 楼主| 发表于 2012-10-10 16:07 | 显示全部楼层
回复

使用道具 举报

发表于 2012-10-10 16:24 | 显示全部楼层
本帖最后由 hwc2ycy 于 2012-10-10 16:26 编辑
  1. Sub 比较()
  2.     Dim dic1 As Object, dic2 As Object
  3.     Dim iRow As Long
  4.     Dim i As Long, arr
  5.     iRow = [f1].End(xlDown).Row
  6.     arr = Range("f1:g" & iRow).Value
  7.     Set dic1 = CreateObject("scripting.dictionary")
  8.     Set dic2 = CreateObject("scripting.dictionary")
  9.    
  10.     For i = 2 To iRow
  11.         If Not dic1.exists(arr(i, 1)) And arr(i, 1) <> 0 Then
  12.             dic1.Add arr(i, 1), i
  13.         End If
  14.         If Not dic2.exists(arr(i, 2)) And arr(i, 2) <> 0 Then
  15.             dic2.Add arr(i, 2), i
  16.         End If
  17.     Next
  18.     arr = dic1.keys
  19.     For i = 0 To UBound(arr)
  20.         If dic1.exists(arr(i)) And dic2.exists(arr(i)) Then
  21.             Cells(Val(dic1(arr(i))), "f").EntireRow.Interior.ColorIndex = 35
  22.             Cells(Val(dic2(arr(i))), "f").EntireRow.Interior.ColorIndex = 35
  23.         End If
  24.     Next
  25. End Sub
复制代码
回复

使用道具 举报

发表于 2012-10-10 16:37 | 显示全部楼层    本楼为最佳答案   
  1. Sub 比较2()
  2.     Dim dic1 As Object, dic2 As Object
  3.     Dim iRow As Long
  4.     Dim i As Long, arr, srg As String, str1 As String, str2 As String
  5.     iRow = [f1].End(xlDown).Row
  6.     arr = Range("f1:g" & iRow).Value
  7.     Set dic1 = CreateObject("scripting.dictionary")
  8.     Set dic2 = CreateObject("scripting.dictionary")
  9.    
  10.     For i = 2 To iRow
  11.         If Not dic1.exists(arr(i, 1)) And arr(i, 1) <> 0 Then
  12.             dic1.Add arr(i, 1), i
  13.         End If
  14.         If Not dic2.exists(arr(i, 2)) And arr(i, 2) <> 0 Then
  15.             dic2.Add arr(i, 2), i
  16.         End If
  17.     Next
  18.     arr = dic1.keys
  19.     For i = 0 To UBound(arr)
  20.         If dic1.exists(arr(i)) And dic2.exists(arr(i)) Then
  21.             str1 = Replace(Cells(Val(dic1(arr(i))), "f").Address, "$", "") & ","
  22.             str2 = Replace(Cells(Val(dic2(arr(i))), "f").Address, "$", "") & ","
  23.             If Len(srg & str1 & str2) > 249 Then
  24.                 Range(Left(srg, Len(srg) - 1)).EntireRow.Interior.ColorIndex = 35
  25.                 srg = str1 & str2
  26.             Else
  27.                 srg = srg & str1 & str2
  28.             End If
  29.         End If
  30.     Next
  31. End Sub
复制代码
回复

使用道具 举报

发表于 2012-10-10 16:44 | 显示全部楼层
代码只匹配第一次相等的行。
回复

使用道具 举报

发表于 2012-10-10 16:47 | 显示全部楼层
0值不匹配。
回复

使用道具 举报

 楼主| 发表于 2012-10-10 16:47 | 显示全部楼层
大部分已标记,还有小部分数据没有标记出来
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 00:22 , Processed in 0.341736 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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