Excel精英培训网

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

[已解决]求多列中,每行数据都完全相同的列的列号?

[复制链接]
发表于 2014-10-19 14:52 | 显示全部楼层 |阅读模式
本帖最后由 403300768 于 2014-10-19 17:38 编辑

求多列中,每行数据都完全相同的列的列号?因这些列的内容不同设置不同颜色。
最佳答案
2014-10-19 15:23
本帖最后由 xdragon 于 2014-10-19 15:29 编辑
  1. Sub test()
  2.     Dim arr, re, d, i%, sr$, col%
  3.     arr = Range("V6:BX9").Value
  4.     ReDim re(1 To 1, 1 To UBound(arr, 2))
  5.     Set d = CreateObject("scripting.dictionary")
  6.     For i = 1 To UBound(arr, 2)
  7.         sr = arr(1, i) & "|" & arr(2, i) & "|" & arr(3, i) & "|" & arr(4, i)
  8.         If d.exists(sr) Then
  9.            col = d(sr)(0) + 21
  10.            re(1, d(sr)(0)) = col
  11.            Union(Range(Cells(6, col), Cells(9, col)), Cells(14, col)).Interior.Color = d(sr)(1)
  12.            d(sr) = Array(i, d(sr)(1))
  13.            re(1, i) = i + 21
  14.            Union(Range(Cells(6, i + 21), Cells(9, i + 21)), Cells(14, i + 21)).Interior.Color = d(sr)(1)
  15.         Else
  16.            d(sr) = Array(i, RGB(Int(Rnd * 256) + 1, Int(Rnd * 256) + 1, Int(Rnd * 256) + 1))
  17.         End If
  18.     Next
  19.     Range("V14:BX14") = re
  20. End Sub
复制代码
颜色是设置的随机色

Book1.zip

5.17 KB, 下载次数: 30

求助

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2014-10-19 14:53 | 显示全部楼层
回复

使用道具 举报

发表于 2014-10-19 15:23 | 显示全部楼层    本楼为最佳答案   
本帖最后由 xdragon 于 2014-10-19 15:29 编辑
  1. Sub test()
  2.     Dim arr, re, d, i%, sr$, col%
  3.     arr = Range("V6:BX9").Value
  4.     ReDim re(1 To 1, 1 To UBound(arr, 2))
  5.     Set d = CreateObject("scripting.dictionary")
  6.     For i = 1 To UBound(arr, 2)
  7.         sr = arr(1, i) & "|" & arr(2, i) & "|" & arr(3, i) & "|" & arr(4, i)
  8.         If d.exists(sr) Then
  9.            col = d(sr)(0) + 21
  10.            re(1, d(sr)(0)) = col
  11.            Union(Range(Cells(6, col), Cells(9, col)), Cells(14, col)).Interior.Color = d(sr)(1)
  12.            d(sr) = Array(i, d(sr)(1))
  13.            re(1, i) = i + 21
  14.            Union(Range(Cells(6, i + 21), Cells(9, i + 21)), Cells(14, i + 21)).Interior.Color = d(sr)(1)
  15.         Else
  16.            d(sr) = Array(i, RGB(Int(Rnd * 256) + 1, Int(Rnd * 256) + 1, Int(Rnd * 256) + 1))
  17.         End If
  18.     Next
  19.     Range("V14:BX14") = re
  20. End Sub
复制代码
颜色是设置的随机色

评分

参与人数 1 +1 收起 理由
403300768 + 1 很给力!

查看全部评分

回复

使用道具 举报

发表于 2014-10-19 15:40 | 显示全部楼层
函数解决列号~
V14单元格复制以下公式,
三键回车(点击公式编辑栏中的任意位置,按住 shift、ctrl 两个键,然后敲enter键 ),
横拉;
=IF(SUM(IF(($V$6:$BX$6=V6)*($V$7:$BX$7=V7)*($V$8:$BX$8=V8)*($V$9:$BX$9=V9),1))>1,COLUMN(),"")

评分

参与人数 1 +1 收起 理由
403300768 + 1 很给力!谢谢高手!!!真厉害!!!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-10-19 16:26 | 显示全部楼层
本帖最后由 403300768 于 2014-10-19 16:29 编辑
xdragon 发表于 2014-10-19 15:23
颜色是设置的随机色

谢谢老师,如果我想在v15:bx15中    同时显示出v6:bx6的数据和相同内容列的颜色,则代码该怎么变?
v15:bx15中
2
12
8
1
5
1
3
4
4
60
1
3
14
5
11
4
6
1
3
1
16
9
10
20
26
2
15
3
8
12
1
3
3
5
2
8
1
3
15
1
1
4
7
3
3
9
8
10
11
16
5
4
4
4
3
回复

使用道具 举报

发表于 2014-10-19 16:36 | 显示全部楼层
  1. Sub test()
  2.     Dim arr, re, d, i%, sr$, col%
  3.     arr = Range("V6:BX9").Value
  4.     ReDim re(1 To 2, 1 To UBound(arr, 2))
  5.     Set d = CreateObject("scripting.dictionary")
  6.     For i = 1 To UBound(arr, 2)
  7.         sr = arr(1, i) & "|" & arr(2, i) & "|" & arr(3, i) & "|" & arr(4, i)
  8.         If d.exists(sr) Then
  9.            col = d(sr)(0) + 21
  10.            re(1, d(sr)(0)) = col
  11.            Union(Range(Cells(6, col), Cells(9, col)), Cells(14, col).Resize(2)).Interior.Color = d(sr)(1)
  12.            d(sr) = Array(i, d(sr)(1))
  13.            re(1, i) = i + 21
  14.            Union(Range(Cells(6, i + 21), Cells(9, i + 21)), Cells(14, i + 21).Resize(2)).Interior.Color = d(sr)(1)
  15.         Else
  16.            d(sr) = Array(i, RGB(Int(Rnd * 256) + 1, Int(Rnd * 256) + 1, Int(Rnd * 256) + 1))
  17.         End If
  18.         re(2, i) = arr(1, i)
  19.     Next
  20.     Range("V14:BX15") = re
  21. End Sub
复制代码
不是很明白你的问题,是这样吗?

评分

参与人数 1 +1 收起 理由
403300768 + 1 很给力!

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-10-19 17:14 | 显示全部楼层
本帖最后由 403300768 于 2014-10-19 17:25 编辑
xdragon 发表于 2014-10-19 16:36
不是很明白你的问题,是这样吗?

是这样的,谢谢你老师。不好意思,再请教一个问题。针对最前的问题:如果数据区域是V6:BX12,结果显示在V18:BX18.那代码是否可以这样改,Sub test()    Dim arr, re, d, i%, sr$, col%
    arr = Range("V6:BX12").Value
    ReDim re(1 To 1, 1 To UBound(arr, 2))
    Set d = CreateObject("scripting.dictionary")
    For i = 1 To UBound(arr, 2)
        sr = arr(1, i) & "|" & arr(2, i) & "|" & arr(3, i) & "|" & arr(4, i)& "|" & arr(5, i)& "|" & arr(6, i)& "|" & arr(7, i)
        If d.exists(sr) Then
           col = d(sr)(0) + 21
           re(1, d(sr)(0)) = col
           Union(Range(Cells(6, col), Cells(12, col)), Cells(18, col)).Interior.Color = d(sr)(1)
           d(sr) = Array(i, d(sr)(1))
           re(1, i) = i + 21
           Union(Range(Cells(6, i + 21), Cells(12, i + 21)), Cells(18, i + 21)).Interior.Color = d(sr)(1)
        Else
           d(sr) = Array(i, RGB(Int(Rnd * 256) + 1, Int(Rnd * 256) + 1, Int(Rnd * 256) + 1))
        End If
    Next
    Range("V18:BX18") = re
End Sub

回复

使用道具 举报

发表于 2014-10-19 17:44 | 显示全部楼层
403300768 发表于 2014-10-19 17:14
是这样的,谢谢你老师。不好意思,再请教一个问题。针对最前的问题:如果数据区域是V6:BX12,结果显示在V ...

是的,cells(row,column)就是单元格的意思,18就是行,range("V18:BX18")=re 就是把re这个数组放在单元格中,所以你想要改只要改代码对应的位置就好了
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-11 15:30 , Processed in 0.273693 second(s), 11 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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